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 will also contain a ‘clientMutationId`
Defined Under Namespace
Classes: Result
Instance Attribute Summary collapse
-
#arguments ⇒ Object
(also: #input_fields)
readonly
Returns the value of attribute arguments.
-
#description ⇒ Object
Returns the value of attribute description.
-
#fields ⇒ Object
(also: #return_fields)
readonly
Returns the value of attribute fields.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
- #field ⇒ Object
-
#initialize ⇒ Mutation
constructor
A new instance of Mutation.
- #input_type ⇒ Object
- #resolve=(proc) ⇒ Object
- #return_type ⇒ Object
Constructor Details
#initialize ⇒ Mutation
Returns a new instance of Mutation.
64 65 66 67 |
# File 'lib/graphql/relay/mutation.rb', line 64 def initialize @fields = {} @arguments = {} end |
Instance Attribute Details
#arguments ⇒ Object (readonly) Also known as: input_fields
Returns the value of attribute arguments.
58 59 60 |
# File 'lib/graphql/relay/mutation.rb', line 58 def arguments @arguments end |
#description ⇒ Object
Returns the value of attribute description.
57 58 59 |
# File 'lib/graphql/relay/mutation.rb', line 57 def description @description end |
#fields ⇒ Object (readonly) Also known as: return_fields
Returns the value of attribute fields.
58 59 60 |
# File 'lib/graphql/relay/mutation.rb', line 58 def fields @fields end |
#name ⇒ Object
Returns the value of attribute name.
57 58 59 |
# File 'lib/graphql/relay/mutation.rb', line 57 def name @name end |
Instance Method Details
#field ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/graphql/relay/mutation.rb', line 73 def field @field ||= begin field_return_type = self.return_type field_input_type = self.input_type field_resolve_proc = -> (obj, args, ctx){ results_hash = @resolve_proc.call(args[:input], ctx) Result.new(arguments: args, result: results_hash) } GraphQL::Field.define do type(field_return_type) argument :input, !field_input_type resolve(field_resolve_proc) end end end |
#input_type ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/graphql/relay/mutation.rb', line 105 def input_type @input_type ||= begin mutation_name = name type_name = "#{mutation_name}Input" type_fields = input_fields GraphQL::InputObjectType.define do name(type_name) description("Autogenerated input type of #{mutation_name}") input_field :clientMutationId, !types.String type_fields.each do |name, field_obj| input_field name, field_obj.type, field_obj.description, default_value: field_obj.default_value end end end end |
#resolve=(proc) ⇒ Object
69 70 71 |
# File 'lib/graphql/relay/mutation.rb', line 69 def resolve=(proc) @resolve_proc = proc end |
#return_type ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/graphql/relay/mutation.rb', line 89 def return_type @return_type ||= begin mutation_name = name type_name = "#{mutation_name}Payload" type_fields = return_fields GraphQL::ObjectType.define do name(type_name) description("Autogenerated return type of #{mutation_name}") field :clientMutationId, !types.String type_fields.each do |name, field_obj| field name, field: field_obj end end end end |