Class: GQL::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/gql/node.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ast_node, target, variables, context) ⇒ Node

Returns a new instance of Node.



65
66
67
68
# File 'lib/gql/node.rb', line 65

def initialize(ast_node, target, variables, context)
  @ast_node, @target = ast_node, target
  @variables, @context = variables, context
end

Instance Attribute Details

#ast_nodeObject (readonly)

Returns the value of attribute ast_node.



63
64
65
# File 'lib/gql/node.rb', line 63

def ast_node
  @ast_node
end

#contextObject (readonly)

Returns the value of attribute context.



63
64
65
# File 'lib/gql/node.rb', line 63

def context
  @context
end

#targetObject (readonly)

Returns the value of attribute target.



63
64
65
# File 'lib/gql/node.rb', line 63

def target
  @target
end

#variablesObject (readonly)

Returns the value of attribute variables.



63
64
65
# File 'lib/gql/node.rb', line 63

def variables
  @variables
end

Class Method Details

.call(id, result_class = nil, body = nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/gql/node.rb', line 13

def call(id, result_class = nil, body = nil)
  if body.nil? && result_class.is_a?(Proc)
    body = result_class
    result_class = nil
  end

  body ||= lambda { |*args| target.public_send(id, *args) }

  call_class = Call.build_class(id, result_class, body)

  self.const_set "#{id.to_s.camelize}Call", call_class
  self.calls = calls.merge(id.to_sym => call_class)
end

.cursor(id = nil, &block) ⇒ Object



45
46
47
48
# File 'lib/gql/node.rb', line 45

def cursor(id = nil, &block)
  body = id ? -> { target.public_send(id) } : block
  field :cursor, { type: Simple }, &body
end

.field(*ids, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/gql/node.rb', line 27

def field(*ids, &block)
  options = ids.extract_options!

  ids.each do |id|
    method = block || lambda { target.public_send(id) }
    field_type = options.delete(:type) || Field

    unless field_type <= Field
      raise Errors::InvalidNodeClass.new(field_type, Field)
    end

    field_class = field_type.build_class(id, method, options)

    self.const_set "#{id.to_s.camelize}Field", field_class
    self.fields = fields.merge(id.to_sym => field_class)
  end
end

.method_missing(method, *ids, &block) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/gql/node.rb', line 50

def method_missing(method, *ids, &block)
  if field_type = GQL.field_types[method]
    options = ids.extract_options!

    field(*ids, options.merge(type: field_type), &block)
  else
    super
  end
rescue NoMethodError => exc
  raise Errors::UndefinedFieldType, method
end

Instance Method Details

#raw_valueObject



119
120
121
# File 'lib/gql/node.rb', line 119

def raw_value
  nil
end

#valueObject



70
71
72
73
74
75
76
77
78
# File 'lib/gql/node.rb', line 70

def value
  if ast_call = ast_node.call
    value_of_call ast_call
  elsif ast_fields = ast_node.fields
    value_of_fields ast_fields
  else
    raw_value
  end
end

#value_of_call(ast_call) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/gql/node.rb', line 80

def value_of_call(ast_call)
  call_class = self.class.calls[ast_call.id]

  if call_class.nil?
    raise Errors::UndefinedCall.new(ast_call.id, self.class)
  end

  call = call_class.new(self, ast_call, target, variables, context)
  call.execute
end

#value_of_field(ast_field) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/gql/node.rb', line 99

def value_of_field(ast_field)
  case ast_field.id
  when :node
    field = self.class.new(ast_field, target, variables, context)
    field.value
  else
    field_class = self.class.fields[ast_field.id]

    if field_class.nil?
      raise Errors::UndefinedField.new(ast_field.id, self.class)
    end

    method = Field::Method.new(target, context)
    target = method.execute(field_class.method)

    field = field_class.new(ast_field, target, variables, context)
    field.value
  end
end

#value_of_fields(ast_fields) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/gql/node.rb', line 91

def value_of_fields(ast_fields)
  ast_fields.reduce({}) do |memo, ast_field|
    key = ast_field.alias_id || ast_field.id

    memo.merge key => value_of_field(ast_field)
  end
end