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



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

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.



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

def ast_node
  @ast_node
end

#contextObject (readonly)

Returns the value of attribute context.



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

def context
  @context
end

#targetObject (readonly)

Returns the value of attribute target.



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

def target
  @target
end

#variablesObject (readonly)

Returns the value of attribute variables.



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

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
74
# File 'lib/gql/node.rb', line 26

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

    proc_or_class = args.shift || -> (*pargs) { target.public_send(id, *pargs) }
    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 |klass|
          klass.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", klass
        end
      else
        proc_or_class
      end

    call_class.id = id.to_s
    call_class.result_class = result_class

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

.cursor(id_or_proc) ⇒ Object



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

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



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

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



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

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
  raise Errors::UndefinedFieldType, method
end

.respond_to?(method, *args) ⇒ Boolean



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

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

.validate_is_subclass!(subclass, name) ⇒ Object



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

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



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

def raw_value
  nil
end

#valueObject



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

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



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

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



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

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



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

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