Class: GraphQL::Schema::RelayClassicMutation

Inherits:
Mutation show all
Includes:
HasSingleInputArgument
Defined in:
lib/graphql/schema/relay_classic_mutation.rb

Overview

Mutations that extend this base class get some conventions added for free:

  • An argument called clientMutationId is 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 clientMutationId to 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:

  • clientMutationId supports 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 one GraphQL variable ($input) instead of making a separate variable for each argument.

See Also:

  • for an example, it's basically the same.

Constant Summary

Constants included from Member::HasFields

Member::HasFields::CONFLICT_FIELD_NAMES, Member::HasFields::GRAPHQL_RUBY_KEYWORDS, Member::HasFields::RUBY_KEYWORDS

Constants included from GraphQL::Schema::Resolver::HasPayloadType

GraphQL::Schema::Resolver::HasPayloadType::NO_INTERFACES

Constants included from Member::HasArguments

Member::HasArguments::NO_ARGUMENTS

Constants included from EmptyObjects

EmptyObjects::EMPTY_ARRAY, EmptyObjects::EMPTY_HASH

Constants included from Member::GraphQLTypeNames

Member::GraphQLTypeNames::Boolean, Member::GraphQLTypeNames::ID, Member::GraphQLTypeNames::Int

Instance Attribute Summary

Attributes inherited from Resolver

#context, #field, #object

Attributes included from Member::BaseDSLMethods

#default_graphql_name, #graphql_name

Instance Method Summary collapse

Methods inherited from Mutation

#call_resolve, visible?

Methods included from Member::HasFields

#add_field, #all_field_definitions, #field, #field_class, #global_id_field, #own_fields

Methods included from GraphQL::Schema::Resolver::HasPayloadType

#field, #field_class, #object_class, #payload_type, #type

Methods inherited from Resolver

all_field_argument_definitions, any_field_arguments?, argument, #arguments, #authorized?, broadcastable, broadcastable?, #call_resolve, complexity, #dataloader, default_page_size, extension, extensions, extras, field_arguments, get_field_argument, has_default_page_size?, has_max_page_size?, #initialize, max_page_size, null, #ready?, #resolve, resolve_method, resolver_method, type, type_expr, #unauthorized_object

Methods included from Member::BaseDSLMethods

#authorized?, #comment, #default_relay, #description, #introspection, #introspection?, #mutation, #name, #visible?

Methods included from Member::HasArguments

#add_argument, #all_argument_definitions, #any_arguments?, #argument, #argument_class, #arguments, #arguments_statically_coercible?, #coerce_arguments, #get_argument, #own_arguments, #remove_argument, #validate_directive_argument

Methods included from Member::HasValidators

#validates, #validators

Methods included from Member::HasPath

#path

Methods included from Member::HasDirectives

add_directive, #directive, #directives, get_directives, #inherited, #remove_directive, remove_directive

Constructor Details

This class inherits a constructor from GraphQL::Schema::Resolver

Instance Method Details

#resolve_with_support(**inputs) ⇒ Object

Override GraphQL::Schema::Resolver#resolve_with_support to delete client_mutation_id from the kwargs.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/graphql/schema/relay_classic_mutation.rb', line 35

def resolve_with_support(**inputs)
  input = inputs[:input].to_kwargs

  if input
    # This is handled by Relay::Mutation::Resolve, a bit hacky, but here we are.
    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