Class: GraphQL::Relay::Mutation

Inherits:
Object
  • Object
show all
Includes:
DefinitionHelpers::DefinedByConfig
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`

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, 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: Result

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#descriptionObject

Returns the value of attribute description


53
54
55
# File 'lib/graphql/relay/mutation.rb', line 53

def description
  @description
end

#input_fieldsObject

Returns the value of attribute input_fields


53
54
55
# File 'lib/graphql/relay/mutation.rb', line 53

def input_fields
  @input_fields
end

#nameObject

Returns the value of attribute name


53
54
55
# File 'lib/graphql/relay/mutation.rb', line 53

def name
  @name
end

#return_fieldsObject

Returns the value of attribute return_fields


53
54
55
# File 'lib/graphql/relay/mutation.rb', line 53

def return_fields
  @return_fields
end

Instance Method Details

#fieldObject


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/graphql/relay/mutation.rb', line 59

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_typeObject


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/graphql/relay/mutation.rb', line 91

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


55
56
57
# File 'lib/graphql/relay/mutation.rb', line 55

def resolve=(proc)
  @resolve_proc = proc
end

#return_typeObject


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/graphql/relay/mutation.rb', line 75

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