Module: GraphQL::Schema::Resolver::HasPayloadType

Included in:
Mutation, Subscription
Defined in:
lib/graphql/schema/resolver/has_payload_type.rb

Overview

Adds field(...) helper to resolvers so that they can generate payload types.

Or, an already-defined one can be attached with payload_type(...).

Constant Summary collapse

NO_INTERFACES =
[].freeze

Instance Method Summary collapse

Instance Method Details

#field(*args, **kwargs, &block) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/graphql/schema/resolver/has_payload_type.rb', line 52

def field(*args, **kwargs, &block)
  pt = payload_type # make sure it's initialized with any inherited fields
  field_defn = super

  # Remove any inherited fields to avoid false conflicts at runtime
  prev_fields = pt.own_fields[field_defn.graphql_name]
  case prev_fields
  when GraphQL::Schema::Field
    if prev_fields.owner != self
      pt.own_fields.delete(field_defn.graphql_name)
    end
  when Array
    prev_fields.reject! { |f| f.owner != self }
    if prev_fields.empty?
      pt.own_fields.delete(field_defn.graphql_name)
    end
  end

  pt.add_field(field_defn, method_conflict_warning: false)
  field_defn
end

#field_class(new_class = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/graphql/schema/resolver/has_payload_type.rb', line 26

def field_class(new_class = nil)
  if new_class
    @field_class = new_class
  elsif defined?(@field_class) && @field_class
    @field_class
  else
    find_inherited_value(:field_class, GraphQL::Schema::Field)
  end
end

#object_class(new_class = nil) ⇒ Class

An object class to use for deriving return types

Parameters:

  • new_class (Class, nil) (defaults to: nil)

    Defaults to Object

Returns:

  • (Class)


39
40
41
42
43
44
45
46
47
48
# File 'lib/graphql/schema/resolver/has_payload_type.rb', line 39

def object_class(new_class = nil)
  if new_class
    if defined?(@payload_type)
      raise "Can't configure `object_class(...)` after the payload type has already been initialized. Move this configuration higher up the class definition."
    end
    @object_class = new_class
  else
    @object_class || find_inherited_value(:object_class, GraphQL::Schema::Object)
  end
end

#payload_type(new_payload_type = nil) ⇒ Class Also known as: type, type_expr

Call this method to get the derived return type of the mutation, or use it as a configuration method to assign a return type instead of generating one.

Parameters:

  • new_payload_type (Class, nil) (defaults to: nil)

    If a type definition class is provided, it will be used as the return type of the mutation field

Returns:

  • (Class)

    The object type which this mutation returns.



16
17
18
19
20
21
# File 'lib/graphql/schema/resolver/has_payload_type.rb', line 16

def payload_type(new_payload_type = nil)
  if new_payload_type
    @payload_type = new_payload_type
  end
  @payload_type ||= generate_payload_type
end