You've specified that the type of the lastName
argument as String!
. The !
indicates the type is non-null -- in other words, the argument is required and cannot equal null.
In your query, you're defining a variable ($lastName
) and assigning it a type. An argument can be passed a variable, but only if their types match. For example, if the argument takes an Int
, you cannot pass it a variable of the type Boolean
. Similarly, if an argument is non-null, you cannot pass it a variable that is nullable. That's because a nullable variable could be null, but that would violate the non-nullability of the argument.
Note that the opposite is not true -- a nullable argument can accept a non-null variable.
This is valid:
# String! argument and String! variable
type Mutation {
signUp(lastName: String!): String!
}
mutation($lastName:String!) {
signUp(lastName:$lastName)
}
Also valid:
# String argument and String! variable
type Mutation {
signUp(lastName: String): String!
}
mutation($lastName: String!) {
signUp(lastName: $lastName)
}
As is this:
# String argument and String variable
type Mutation {
signUp(lastName: String): String!
}
mutation($lastName: String) {
signUp(lastName: $lastName)
}
But this is not valid:
# String! argument and String variable
type Mutation {
signUp(lastName: String!): String!
}
mutation($lastName: String) {
signUp(lastName: $lastName)
}
The only exception to this is if you provide a default value for the variable. So this is still valid:
# String! argument and String variable
type Mutation {
signUp(lastName: String!): String!
}
mutation($lastName: String = "Some default value") {
signUp(lastName: $lastName)
}
You can read about variable validation in the spec.