Class: GQL::Node
- Inherits:
-
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.
138
139
140
141
|
# File 'lib/gql/node.rb', line 138
def initialize(ast_node, target, variables, context)
@ast_node, @target = ast_node, target
@variables, @context = variables, context
end
|
Instance Attribute Details
Returns the value of attribute ast_node.
136
137
138
|
# File 'lib/gql/node.rb', line 136
def ast_node
@ast_node
end
|
Returns the value of attribute context.
136
137
138
|
# File 'lib/gql/node.rb', line 136
def context
@context
end
|
Returns the value of attribute target.
136
137
138
|
# File 'lib/gql/node.rb', line 136
def target
@target
end
|
#variables ⇒ Object
Returns the value of attribute variables.
136
137
138
|
# File 'lib/gql/node.rb', line 136
def variables
@variables
end
|
Class Method Details
.build_class(id, proc, options = {}) ⇒ Object
27
28
29
30
31
32
|
# File 'lib/gql/node.rb', line 27
def build_class(id, proc, options = {})
Class.new(self).tap do |field_class|
field_class.id = id.to_s
field_class.proc = proc
end
end
|
.call(id, *args) ⇒ Object
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
75
76
77
78
79
80
81
82
|
# File 'lib/gql/node.rb', line 34
def call(id, *args)
if id.is_a? Hash
id.each do |name, call_class|
call name, call_class
end
else
options = args.
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
97
98
99
100
101
102
|
# File 'lib/gql/node.rb', line 97
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: Raw
end
|
.field(id, *args) ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/gql/node.rb', line 84
def field(id, *args)
options = args.
proc = args.shift || instance_exec(id, &default_proc)
type = options.delete(:type) || Node
Node.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
# File 'lib/gql/node.rb', line 118
def method_missing(method, *args, &block)
if type = GQL.field_types[method]
Node.define_singleton_method method do |*margs, &mblock|
options = margs..merge(type: type)
margs = margs.push(options)
field(*margs, &mblock)
end
send method, *args, &block
else
super
end
rescue NoMethodError
raise Errors::UndefinedFieldType, method
end
|
.respond_to?(method, *args) ⇒ Boolean
114
115
116
|
# File 'lib/gql/node.rb', line 114
def respond_to?(method, *args)
super || GQL.field_types.has_key?(method)
end
|
.validate_is_subclass!(subclass, name) ⇒ Object
104
105
106
107
108
109
110
111
112
|
# File 'lib/gql/node.rb', line 104
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_value ⇒ Object
191
192
193
|
# File 'lib/gql/node.rb', line 191
def raw_value
nil
end
|
143
144
145
146
147
148
149
150
151
|
# File 'lib/gql/node.rb', line 143
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
153
154
155
156
157
158
159
160
161
|
# File 'lib/gql/node.rb', line 153
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_class.execute(self.class, ast_call, target, variables, context)
end
|
#value_of_field(ast_field) ⇒ Object
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
# File 'lib/gql/node.rb', line 171
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
163
164
165
166
167
168
169
|
# File 'lib/gql/node.rb', line 163
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
|