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(...).

@requires_ancestor: singleton(GraphQL::Schema::Resolver) @requires_ancestor: GraphQL::Schema::Member::HasFields

Constant Summary collapse

NO_INTERFACES =
[].freeze

Instance Method Summary collapse

Instance Method Details

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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/graphql/schema/resolver/has_payload_type.rb', line 85

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



50
51
52
53
54
55
56
57
58
# File 'lib/graphql/schema/resolver/has_payload_type.rb', line 50

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) ⇒ Object

An object class to use for deriving return types

Parameters

Returns

  • Class

:call-seq:

object_class(Class | nil new_class) -> Class


72
73
74
75
76
77
78
79
80
81
# File 'lib/graphql/schema/resolver/has_payload_type.rb', line 72

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) ⇒ Object Also known as: 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) — 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.

:call-seq:

payload_type(Class | nil new_payload_type) -> Class


30
31
32
33
34
35
# File 'lib/graphql/schema/resolver/has_payload_type.rb', line 30

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

#type(new_type = nil, null: nil) ⇒ Object



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

def type(new_type = nil, null: nil)
  if new_type
    payload_type(new_type)
    if !null.nil?
      self.null(null)
    end
  else
    super()
  end
end