question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot make optional mutation argument

See original GitHub issue

According to the docs args are optional by default:

class RechargeSim(graphene.Mutation):
    class Arguments:
        msisdn = graphene.String(required=True)
        network_id = graphene.String(required=True)
        product_id = graphene.String()
        airtime_amount = graphene.Float()

Here is a query:

           result = authed_graphql_client.execute(
                '''
                mutation($msisdn: String!, $networkId: String!, $productId: String!) {
                  rechargeSim(msisdn: $msisdn,
                              networkId: $networkId,
                              productId: $productId) {
                    rechargeId
                    message
                  }
                }
                ''',
                variable_values={
                    'msisdn': sim.msisdn,
                    'networkId': network_id,
                    'productId': product_id
                }
            )

I get this error: TypeError: mutate() missing 1 required positional argument: 'airtime_amount'

I have tried the following variations:

class RechargeSim(graphene.Mutation):
    class Arguments:
        msisdn = graphene.String(required=True)
        network_id = graphene.String(required=True)
        product_id = graphene.String()
        airtime_amount = graphene.Float(required=False)```

class RechargeSim(graphene.Mutation):
    class Arguments:
        msisdn = graphene.String(required=True)
        network_id = graphene.String(required=True)
        product_id = graphene.String()
        airtime_amount = graphene.Float(default_value=None)```

class RechargeSim(graphene.Mutation):
    class Arguments:
        msisdn = graphene.String(required=True)
        network_id = graphene.String(required=True)
        product_id = graphene.String()
        airtime_amount = graphene.Float(required=False, default_value=None)```

In the end I have hacked around this by defaulting the value to 0 and then resetting it to None in the resolver if 0.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

23reactions
jkimbocommented, Jun 15, 2018

Yep so because airtime_amount is an optional field, when it is not provided in your mutation it won’t be passed to your mutate function and that is causing the error. Replace your mutate function def with:

def mutate(root, info, msisdn, network_id, **kwargs):
	product_id = kwargs.get('product_id', None)
	airtime_amount = kwargs.get('airtime_amount', None)

The reason why it’s not passed to your mutate function if it’s not defined is so that you can differentiate between it being ‘undefined’ and it being null.

1reaction
jonahmolinskicommented, May 5, 2020

@larsblumberg Seconded. Null values are dropped and there is no way to differentiate between null and undefined.

@jkimbo Has something changed that has caused the functionality of undefined/null to stop working?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Optional argument - Mutation - TypeGraphQL - Stack Overflow
To make an argument optional, pass a second parameter to the @Arg decorate like this: @Arg('firstName', { nullable: true }) firstName: ...
Read more >
Passing optional arguments to Mutation for connections
I am trying to write a mutation resolver function for passing an optional argument for a comment to reference either null if no ......
Read more >
Querying with GraphQL optional arguments in Elm - Thoughtbot
We update the GraphQL schema to have a nullable notes field. The notes argument is optional. We can tell because it doesn't have...
Read more >
Optional Undefined Arguments | GraphQL Kotlin
In the GraphQL world, input types can be optional which means that the client can either: Not specify a value at all; Send...
Read more >
6.4. Mutations should clearly describe all the ... - GraphQL Rules
In that case, we cannot make both arguments in our mutation mandatory. The fact, that required argument is not sent we will know...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found