Module: GraphQL::Define::AssignObjectField

Defined in:
lib/graphql/define/assign_object_field.rb

Overview

Turn field configs into a Field and attach it to a ObjectType or InterfaceType

Class Method Summary collapse

Class Method Details

.call(owner_type, name, type_or_field = nil, desc = nil, function: nil, field: nil, relay_mutation_function: nil, **kwargs, &block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/graphql/define/assign_object_field.rb', line 6

def self.call(owner_type, name, type_or_field = nil, desc = nil, function: nil, field: nil, relay_mutation_function: nil, **kwargs, &block)
  name_s = name.to_s

  # Move some positional args into keywords if they're present
  desc && kwargs[:description] ||= desc
  name && kwargs[:name] ||= name_s

  if !type_or_field.nil? && !type_or_field.is_a?(GraphQL::Field)
    # Maybe a string, proc or BaseType
    kwargs[:type] = type_or_field
  end

  base_field = if type_or_field.is_a?(GraphQL::Field)
    type_or_field.redefine(name: name_s)
  elsif function
    GraphQL::Field.define(
      arguments: function.arguments,
      complexity: function.complexity,
      name: name_s,
      type: function.type,
      resolve: function,
      description: function.description,
      function: function,
      deprecation_reason: function.deprecation_reason,
    )
  elsif field.is_a?(GraphQL::Field)
    field.redefine(name: name_s)
  else
    nil
  end

  field = if base_field
    base_field.redefine(kwargs, &block)
  else
    GraphQL::Field.define(kwargs, &block)
  end


  # Attach the field to the type
  owner_type.fields[name_s] = field
end