GraphQL mutation variables

I am trying to make a simple mutation using GraphQL with the GraphiQL interface. My mutation looks like this:

mutation M($name: String) { addGroup(name:$name) { id, name } } 

with variables:

 { "name": "ben" } 

But this gives me an error: Variable $name of type "String" used in position expecting type "String!"

If I change my mutation to mutation M($name: String = "default") , it will work as expected. This seems to be related to the type system, but I cannot figure out what the problem is.

+7
graphql graphql-js
source share
2 answers

You probably defined the input name as a non-zero string (something like type: new GraphQLNonNull(GraphQLString) when using a js server or String! In a simple graphical language).

Thus, your entry into the mutation must match, which also means a non-empty string. If you go to the following, you should work:

 mutation M($name: String!) { addGroup(name:$name) { id, name } } 

Also, if you define a default value just like you do, because it is an empty string.

Finally, you can refuse the requirement to be non-zero on the server.

+6
source share

I think in you mutation addGroup() args for name is of type String! , i.e. new GraphQLNonNull(GraphQLString) , but in your mutation you specify as String , which conflicts with the type system.

0
source share

All Articles