Cannot make optional mutation argument
See original GitHub issueAccording 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:
- Created 5 years ago
- Reactions:4
- Comments:7 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yep so because
airtime_amount
is an optional field, when it is not provided in your mutation it won’t be passed to yourmutate
function and that is causing the error. Replace yourmutate
function def with: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.
@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?