Home > Mobile >  What is `null false` in mutations graphQL rails?
What is `null false` in mutations graphQL rails?

Time:04-23

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.

CodePudding user response:

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) ⇒ Object

If true (default), then the return type for this resolver will be nullable. If false, then the return type is non-null.

CodePudding user response:

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!
}
  • Related