Class: GraphQL::FieldDefiner

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/field_definer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner_class) ⇒ FieldDefiner

Returns a new instance of FieldDefiner.



3
4
5
# File 'lib/graphql/field_definer.rb', line 3

def initialize(owner_class)
  @owner_class = owner_class
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/graphql/field_definer.rb', line 7

def method_missing(method_name, *args, &block)
  type = GraphQL::SCHEMA.get_field(method_name)
  if type.present?
    create_field(args[0], type: type, description: args[1])
  else
    super
  end
end

Instance Attribute Details

#owner_classObject (readonly)

Returns the value of attribute owner_class.



2
3
4
# File 'lib/graphql/field_definer.rb', line 2

def owner_class
  @owner_class
end

Instance Method Details

#create_field(field_name, type: nil, description: nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/graphql/field_definer.rb', line 16

def create_field(field_name, type: nil, description: nil)
  field_name = field_name.to_s
  field_class = GraphQL::Field.create_class({
    name: field_name,
    type: type,
    owner_class: owner_class,
    description: description,
  })

  field_class_name = field_name.camelize + "Field"
  owner_class.const_set(field_class_name, field_class)
  owner_class.own_fields[field_name] = field_class
end