'What is `null false` in mutations graphQL rails?
module Mutations
class BaseMutation < GraphQL::Schema::Mutation
null false # <---- what is this?
end
end
In a lot of classes, I see this function null(false) or null(true) but I don't find any information about this in the rails graphql docs.
Solution 1:[1]
It lets you make the resolver (or mutation) nullable/non-nullable. Documented here (note that GraphQL::Schema::Mutation inherits from GraphQL::Schema::Resolver since Mutation is a type of Resolver):
.null(allow_null = nil) ? ObjectIf
true(default), then the return type for this resolver will be nullable. Iffalse, then the return type is non-null.
Solution 2:[2]
Specifically, it's used to indicate whether the payload object (which is auto-generated if you extend GraphQL::Schema::Mutation) is nullable.
If it's set to null: true, then the mutation may return no results at all rather than the payload object.
If you have a resolver named CreateMessageResolver and a mutation class named MessageResolverType with null: true, then the generated schema for your mutation type will look like this:
type Mutation {
createMessage(): CreateMessagePayload
}
If null: false, it will look like this:
type Mutation {
createMessage(): CreateMessagePayload!
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dogbert |
| Solution 2 | TonyArra |
