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 GraphQL::Schema::Mutation 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 EmptyObjects

EmptyObjects::EMPTY_ARRAY, EmptyObjects::EMPTY_HASH

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 Member::GraphQLTypeNames

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

Instance Attribute Summary

Attributes inherited from Resolver

#context, #exec_index, #exec_result, #field, #field_resolve_step, #object, #prepared_arguments, #raw_arguments

Attributes included from Member::BaseDSLMethods

#default_graphql_name, #graphql_name

Attributes included from Member::HasDeprecationReason

#deprecation_reason

Instance Method Summary collapse

Methods included from HasSingleInputArgument

included

Methods inherited from Mutation

#call_resolve, visible?

Methods included from Member::HasFields

#add_field, #all_field_definitions, extended, #field, #field_class, #global_id_field, #has_no_fields, #has_no_fields?, included, #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?, authorizes?, broadcastable, broadcastable?, #call_resolve, complexity, default_page_size, extension, extensions, extras, field_arguments, get_field_argument, has_default_page_size?, has_max_page_size?, inherited, #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, #visible?

Methods included from FindInheritedValue

extended, included

Methods included from Member::HasArguments

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

Methods included from Member::HasAuthorization

#authorized?, extended, included

Methods included from Member::HasAuthorization::ClassConfigured

#authorizes?

Methods included from Member::HasValidators

extended, #validates, #validators

Methods included from Member::HasPath

#path

Methods included from Member::HasDirectives

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

Methods included from Member::HasDeprecationReason

extended

Methods included from Member::HasDataloader

#dataload, #dataload_all, #dataload_all_associations, #dataload_all_records, #dataload_association, #dataload_record, #dataloader

Constructor Details

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

Instance Method Details

#call ⇒ Object

: -> void



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/graphql/schema/relay_classic_mutation.rb', line 57

def call
  # The runtime prepares arguments before calling this
  prepared_arguments = @prepared_arguments #: as !nil
  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

#resolve_with_support(**inputs) ⇒ Object

delete client_mutation_id from the kwargs. : (**untyped inputs) -> untyped



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 36

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