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_field`s

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

Instance Method Summary collapse

Methods included from Define::InstanceDefinable

#definition_proc=, included, #metadata

Constructor Details

#initializeMutation

Returns a new instance of Mutation.



64
65
66
67
# File 'lib/graphql/relay/mutation.rb', line 64

def initialize
  @fields = {}
  @arguments = {}
end

Instance Method Details

#fieldObject



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

def field
  @field ||= begin
    ensure_defined
    relay_mutation = self
    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(relay_mutation.return_type)
      description(relay_mutation.description)
      argument :input, !relay_mutation.input_type
      resolve(field_resolve_proc)
      mutation(relay_mutation)
    end
  end
end

#input_typeObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/graphql/relay/mutation.rb', line 108

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=(proc) ⇒ Object



69
70
71
72
# File 'lib/graphql/relay/mutation.rb', line 69

def resolve=(proc)
  ensure_defined
  @resolve_proc = proc
end

#return_typeObject



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

def return_type
  @return_type ||= begin
    ensure_defined
    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."
      relay_mutation.return_fields.each do |name, field_obj|
        field name, field: field_obj
      end
      mutation(relay_mutation)
    end
  end
end