Class: GraphQL::Relay::Mutation

Inherits:
Object
  • Object
show all
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_fields

Inputs may also contain a clientMutationId

Examples:

Updating the name of an item

UpdateNameMutation = GraphQL::Relay::Mutation.define do
  name "UpdateName"

  input_field :name, !types.String
  input_field :itemId, !types.ID

  return_field :item, ItemType

  resolve ->(inputs, ctx) {
    item = Item.find_by_id(inputs[:id])
    item.update(name: inputs[:name])
    {item: item}
  }
end

MutationType = GraphQL::ObjectType.define do
  # The mutation object exposes a field:
  field :updateName, field: UpdateNameMutation.field
end

# Then query it:
query_string = %|
  mutation updateName {
    updateName(input: {itemId: 1, name: "new name", clientMutationId: "1234"}) {
      item { name }
      clientMutationId
  }|

 GraphQL::Query.new(MySchema, query_string).result
 # {"data" => {
 #   "updateName" => {
 #     "item" => { "name" => "new name"},
 #     "clientMutationId" => "1234"
 #   }
 # }}

Defined Under Namespace

Classes: MutationResolve, Result

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Define::InstanceDefinable

#define, #metadata, #redefine

Constructor Details

#initializeMutation

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 Attribute Details

#argumentsObject 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

#descriptionObject

Returns the value of attribute description


58
59
60
# File 'lib/graphql/relay/mutation.rb', line 58

def description
  @description
end

#fieldsObject 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

#nameObject

Returns the value of attribute name


58
59
60
# File 'lib/graphql/relay/mutation.rb', line 58

def name
  @name
end

#return_typeObject

Returns the value of attribute return_type


58
59
60
# File 'lib/graphql/relay/mutation.rb', line 58

def return_type
  @return_type
end

Instance Method Details

#fieldObject


81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/graphql/relay/mutation.rb', line 81

def field
  @field ||= begin
    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

Returns:

  • (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_typeObject


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/graphql/relay/mutation.rb', line 111

def input_type
  @input_type ||= begin
    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|
        kwargs = {}
        kwargs[:default_value] = field_obj.default_value if field_obj.default_value?
        input_field input_field_name, field_obj.type, field_obj.description, **kwargs
      end
      mutation(relay_mutation)
    end
  end
end

#resolve=(new_resolve_proc) ⇒ Object


77
78
79
# File 'lib/graphql/relay/mutation.rb', line 77

def resolve=(new_resolve_proc)
  @resolve_proc = MutationResolve.new(self, new_resolve_proc, wrap_result: has_generated_return_type?)
end

#result_classObject


128
129
130
131
132
# File 'lib/graphql/relay/mutation.rb', line 128

def result_class
  @result_class ||= begin
    Result.define_subclass(self)
  end
end