Class: GQL::Node

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

Defined Under Namespace

Classes: ExecutionContext

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.



124
125
126
127
# File 'lib/gql/node.rb', line 124

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.



122
123
124
# File 'lib/gql/node.rb', line 122

def ast_node
  @ast_node
end

#contextObject (readonly)

Returns the value of attribute context.



122
123
124
# File 'lib/gql/node.rb', line 122

def context
  @context
end

#targetObject (readonly)

Returns the value of attribute target.



122
123
124
# File 'lib/gql/node.rb', line 122

def target
  @target
end

#variablesObject (readonly)

Returns the value of attribute variables.



122
123
124
# File 'lib/gql/node.rb', line 122

def variables
  @variables
end

Class Method Details

.call(id, *args) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/gql/node.rb', line 26

def call(id, *args)
  if id.is_a? Hash
    id.each do |id, call_class|
      call id, call_class
    end
  else
    options = args.extract_options!

    proc_or_class = args.shift || -> (*args) { target.public_send(id, *args) }
    result_class = options[:returns] || proc_or_class.try(:result_class)

    if result_class.is_a? ::Array
      if result_class.size == 1
        result_class.unshift GQL.default_list_class || Connection
      end

      options = {
        list_class: result_class.first,
        item_class: result_class.last
      }

      result_class = Connection.build_class(:result, nil, options)
    elsif result_class
      Node.validate_is_subclass! result_class, 'result'
    end

    call_class =
      if proc_or_class.is_a? Proc
        Class.new(Call).tap do |call_class|
          call_class.class_eval do
            self.proc = proc_or_class

            def execute(*args)
              instance_exec(*args, &self.class.proc)
            end
          end

          self.const_set "#{id.to_s.camelize}Call", call_class
        end
      else
        proc_or_class
      end

    call_class.result_class = result_class

    self.calls = calls.merge(id.to_sym => call_class)
  end
end

.cursor(id_or_proc) ⇒ Object



88
89
90
91
92
93
# File 'lib/gql/node.rb', line 88

def cursor(id_or_proc)
  id = id_or_proc.is_a?(Proc) ? nil : id_or_proc
  proc = id ? -> { target.public_send(id) } : id_or_proc

  field :cursor, proc, type: Simple
end

.field(id, *args) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/gql/node.rb', line 75

def field(id, *args)
  options = args.extract_options!
  proc    = args.shift || -> { target.public_send(id) }
  type    = options.delete(:type) || Field

  Field.validate_is_subclass! type, 'type'

  type.build_class(id, proc, options).tap do |field_class|
    self.const_set "#{id.to_s.camelize}Field", field_class
    self.fields = fields.merge(id.to_sym => field_class)
  end
end

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



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/gql/node.rb', line 109

def method_missing(method, *args, &block)
  if type = GQL.field_types[method]
    options = args.extract_options!.merge(type: type)

    field(*args.push(options), &block)
  else
    super
  end
rescue NoMethodError => exc
  raise Errors::UndefinedFieldType, method
end

.respond_to?(method, *args) ⇒ Boolean

Returns:



105
106
107
# File 'lib/gql/node.rb', line 105

def respond_to?(method, *args)
  GQL.field_types.has_key?(method) || super
end

.validate_is_subclass!(subclass, name) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/gql/node.rb', line 95

def validate_is_subclass!(subclass, name)
  if subclass.nil?
    raise Errors::UndefinedNodeClass.new(self, name)
  end

  unless subclass <= self
    raise Errors::InvalidNodeClass.new(subclass, self)
  end
end

Instance Method Details

#raw_valueObject



178
179
180
# File 'lib/gql/node.rb', line 178

def raw_value
  nil
end

#valueObject



129
130
131
132
133
134
135
136
137
# File 'lib/gql/node.rb', line 129

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



139
140
141
142
143
144
145
146
147
148
# File 'lib/gql/node.rb', line 139

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(target, context)
  call.result_for self.class, ast_call, variables
end

#value_of_field(ast_field) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/gql/node.rb', line 158

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 = ExecutionContext.new(target, context)
    target = method.execute(field_class.proc)

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

#value_of_fields(ast_fields) ⇒ Object



150
151
152
153
154
155
156
# File 'lib/gql/node.rb', line 150

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

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