class GraphQL::Schema::RelayClassicMutation
Mutations that extend this base class get some conventions added for free:
-
An argument called
clientMutationIdis always added, but it’s not passed to the resolve method. The value is re-inserted to the response. (It’s for client libraries to manage optimistic updates.) -
The returned object type always has a field called
clientMutationIdto support that. -
The mutation accepts one argument called
input,arguments defined in the mutation class are added to that input object, which is generated by the mutation.
These conventions were first specified by Relay Classic, but they come in handy:
-
clientMutationIdsupports optimistic updates and cache rollbacks on the client -
using a single
input:argument makes it easy to post whole JSON objects to the mutation using oneGraphQLvariable ($input) instead of making a separate variable for each argument.
See GraphQL::Schema::Mutation for an example, it’s basically the same.
Public Instance Methods
Source
# File lib/graphql/schema/relay_classic_mutation.rb, line 54 def call input = @prepared_arguments[:input]&.to_kwargs if input client_mutation_id = input.delete(:client_mutation_id) @prepared_arguments[:input] = input end super if (return_value = exec_result[exec_index]).is_a?(Hash) return_value[:client_mutation_id] = client_mutation_id end end
GraphQL::Schema::HasSingleInputArgument#call
# File lib/graphql/schema/relay_classic_mutation.rb, line 34 def resolve_with_support(**inputs) input = inputs[:input].to_kwargs if input input_kwargs = input.to_h client_mutation_id = input_kwargs.delete(:client_mutation_id) inputs[:input] = input_kwargs end return_value = super(**inputs) context.query.after_lazy(return_value) do |return_hash| # It might be an error if return_hash.is_a?(Hash) return_hash[:client_mutation_id] = client_mutation_id end return_hash end end
Override resolve_with_support to delete client_mutation_id from the kwargs.
GraphQL::Schema::HasSingleInputArgument#resolve_with_support