Class: GraphQL::Relay::Mutation
- Inherits:
-
Object
- Object
- GraphQL::Relay::Mutation
- Includes:
- Define::InstanceDefinable
- Defined in:
- lib/graphql/relay/mutation.rb
Overview
Define a Relay mutation:
- give it a name (used for derived inputs & outputs)
- declare its inputs
- declare its outputs
- declare the mutation procedure
‘resolve` should return a hash with a key for each of the `return_field`s
Inputs may also contain a ‘clientMutationId`
Defined Under Namespace
Classes: DeprecatedMutationResolve, MutationResolve, Result
Instance Method Summary collapse
- #field ⇒ Object
- #has_generated_return_type? ⇒ Boolean
-
#initialize ⇒ Mutation
constructor
A new instance of Mutation.
- #input_type ⇒ Object
- #resolve=(new_resolve_proc) ⇒ Object
- #result_class ⇒ Object
- #return_type ⇒ Object
Methods included from Define::InstanceDefinable
#define, #definition_proc=, included, #metadata
Constructor Details
#initialize ⇒ Mutation
Returns a new instance of Mutation.
65 66 67 68 69 |
# File 'lib/graphql/relay/mutation.rb', line 65 def initialize @fields = {} @arguments = {} @has_generated_return_type = false end |
Instance Method Details
#field ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/graphql/relay/mutation.rb', line 89 def field @field ||= begin ensure_defined relay_mutation = self field_resolve_proc = @resolve_proc GraphQL::Field.define do type(relay_mutation.return_type) description(relay_mutation.description) argument :input, !relay_mutation.input_type resolve(field_resolve_proc) mutation(relay_mutation) end end end |
#has_generated_return_type? ⇒ Boolean
71 72 73 74 75 |
# File 'lib/graphql/relay/mutation.rb', line 71 def has_generated_return_type? # Trigger the generation of the return type, if it is dynamically generated: return_type @has_generated_return_type end |
#input_type ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/graphql/relay/mutation.rb', line 121 def input_type @input_type ||= begin ensure_defined relay_mutation = self GraphQL::InputObjectType.define do name("#{relay_mutation.name}Input") description("Autogenerated input type of #{relay_mutation.name}") input_field :clientMutationId, types.String, "A unique identifier for the client performing the mutation." relay_mutation.input_fields.each do |input_field_name, field_obj| input_field input_field_name, field_obj.type, field_obj.description, default_value: field_obj.default_value end mutation(relay_mutation) end end end |
#resolve=(new_resolve_proc) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/graphql/relay/mutation.rb', line 77 def resolve=(new_resolve_proc) ensure_defined resolve_arity = get_arity(new_resolve_proc) if resolve_arity == 2 warn("Mutation#resolve functions should be defined with three arguments: (root_obj, input, context). Two-argument mutation resolves are deprecated.") new_resolve_proc = DeprecatedMutationResolve.new(new_resolve_proc) end @resolve_proc = MutationResolve.new(self, new_resolve_proc, wrap_result: has_generated_return_type?) end |
#result_class ⇒ Object
137 138 139 140 141 142 |
# File 'lib/graphql/relay/mutation.rb', line 137 def result_class @result_class ||= begin ensure_defined Result.define_subclass(self) end end |
#return_type ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/graphql/relay/mutation.rb', line 104 def return_type ensure_defined @return_type ||= begin @has_generated_return_type = true relay_mutation = self GraphQL::ObjectType.define do name("#{relay_mutation.name}Payload") description("Autogenerated return type of #{relay_mutation.name}") field :clientMutationId, types.String, "A unique identifier for the client performing the mutation.", property: :client_mutation_id relay_mutation.return_fields.each do |name, field_obj| field name, field: field_obj end mutation(relay_mutation) end end end |