Class: GraphQL::Query::Context
- Inherits:
-
Object
- Object
- GraphQL::Query::Context
show all
- Extended by:
- Forwardable
- Defined in:
- lib/graphql/query/context.rb,
lib/graphql/query/context/scoped_context.rb
Overview
Expose some query-specific info to field resolve functions.
It delegates [] to the hash that's passed to GraphQL::Query#initialize.
Defined Under Namespace
Classes: ExecutionErrors, Scoped, ScopedContext
Constant Summary
collapse
- RUNTIME_METADATA_KEYS =
Set.new([:current_object, :current_arguments, :current_field, :current_path])
- UNSPECIFIED_FETCH_DEFAULT =
Object.new
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(query:, schema: query.schema, values:) ⇒ Context
Make a new context which delegates key lookup to values
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/graphql/query/context.rb', line 45
def initialize(query:, schema: query.schema, values:)
@query = query
@schema = schema
@provided_values = values || {}
@storage = Hash.new { |h, k| h[k] = {} }
@storage[nil] = @provided_values
@errors = []
@scoped_context = ScopedContext.new(self)
end
|
Instance Attribute Details
34
35
36
|
# File 'lib/graphql/query/context.rb', line 34
def errors
@errors
end
|
#interpreter=(value) ⇒ Object
This method is part of a private API.
You should avoid using this method if possible, as it may be removed or be changed in the future.
67
68
69
|
# File 'lib/graphql/query/context.rb', line 67
def interpreter=(value)
@interpreter = value
end
|
37
38
39
|
# File 'lib/graphql/query/context.rb', line 37
def query
@query
end
|
40
41
42
|
# File 'lib/graphql/query/context.rb', line 40
def schema
@schema
end
|
#scoped_context ⇒ Object
This method is part of a private API.
You should avoid using this method if possible, as it may be removed or be changed in the future.
73
74
75
|
# File 'lib/graphql/query/context.rb', line 73
def scoped_context
@scoped_context
end
|
#types ⇒ Object
81
82
83
|
# File 'lib/graphql/query/context.rb', line 81
def types
@types ||= @query.types
end
|
#value=(value) ⇒ Object
This method is part of a private API.
You should avoid using this method if possible, as it may be removed or be changed in the future.
70
71
72
|
# File 'lib/graphql/query/context.rb', line 70
def value=(value)
@value = value
end
|
214
215
216
|
# File 'lib/graphql/query/context.rb', line 214
def warden
@warden ||= (@query && @query.warden)
end
|
Instance Method Details
#[](key) ⇒ Object
Lookup key from the hash passed to Schema#execute as context:
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/graphql/query/context.rb', line 92
def [](key)
if @scoped_context.key?(key)
@scoped_context[key]
elsif @provided_values.key?(key)
@provided_values[key]
elsif RUNTIME_METADATA_KEYS.include?(key)
if key == :current_path
current_path
else
(current_runtime_state = Fiber[:__graphql_runtime_info]) &&
(query_runtime_state = current_runtime_state[@query]) &&
(query_runtime_state.public_send(key))
end
else
nil
end
end
|
#[]=(key, value) ⇒ Object
Reassign key to the hash passed to Schema#execute as context:
88
89
90
|
# File 'lib/graphql/query/context.rb', line 88
def []=(key, value)
@provided_values[key] = value
end
|
#add_error(error) ⇒ void
This method returns an undefined value.
Add error at query-level.
120
121
122
123
124
125
126
|
# File 'lib/graphql/query/context.rb', line 120
def add_error(error)
if !error.is_a?(ExecutionError)
raise TypeError, "expected error to be a ExecutionError, but was #{error.class}"
end
errors << error
nil
end
|
Returns The backtrace for this point in query execution.
132
133
134
|
# File 'lib/graphql/query/context.rb', line 132
def backtrace
GraphQL::Backtrace.new(self)
end
|
#current_path ⇒ Object
140
141
142
143
144
145
146
147
148
149
150
151
152
|
# File 'lib/graphql/query/context.rb', line 140
def current_path
current_runtime_state = Fiber[:__graphql_runtime_info]
query_runtime_state = current_runtime_state && current_runtime_state[@query]
path = query_runtime_state &&
(result = query_runtime_state.current_result) &&
(result.path)
if path && (rn = query_runtime_state.current_result_name)
path = path.dup
path.push(rn)
end
path
end
|
#dataloader ⇒ Object
62
63
64
|
# File 'lib/graphql/query/context.rb', line 62
def dataloader
@dataloader ||= self[:dataloader] || (query.multiplex ? query.multiplex.dataloader : schema.dataloader_class.new)
end
|
#delete(key) ⇒ Object
154
155
156
157
158
159
160
|
# File 'lib/graphql/query/context.rb', line 154
def delete(key)
if @scoped_context.key?(key)
@scoped_context.delete(key)
else
@provided_values.delete(key)
end
end
|
#dig(key, *other_keys) ⇒ Object
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
# File 'lib/graphql/query/context.rb', line 182
def dig(key, *other_keys)
if RUNTIME_METADATA_KEYS.include?(key)
(current_runtime_state = Fiber[:__graphql_runtime_info]) &&
(query_runtime_state = current_runtime_state[@query]) &&
(obj = query_runtime_state.public_send(key)) &&
if other_keys.empty?
obj
else
obj.dig(*other_keys)
end
elsif @scoped_context.key?(key)
@scoped_context.dig(key, *other_keys)
else
@provided_values.dig(key, *other_keys)
end
end
|
#execution_errors ⇒ Object
136
137
138
|
# File 'lib/graphql/query/context.rb', line 136
def execution_errors
@execution_errors ||= ExecutionErrors.new(self)
end
|
#fetch(key, default = UNSPECIFIED_FETCH_DEFAULT) ⇒ Object
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
# File 'lib/graphql/query/context.rb', line 164
def fetch(key, default = UNSPECIFIED_FETCH_DEFAULT)
if RUNTIME_METADATA_KEYS.include?(key)
(runtime = Fiber[:__graphql_runtime_info]) &&
(query_runtime_state = runtime[@query]) &&
(query_runtime_state.public_send(key))
elsif @scoped_context.key?(key)
scoped_context[key]
elsif @provided_values.key?(key)
@provided_values[key]
elsif default != UNSPECIFIED_FETCH_DEFAULT
default
elsif block_given?
yield(self, key)
else
raise KeyError.new(key: key)
end
end
|
#inspect ⇒ Object
241
242
243
|
# File 'lib/graphql/query/context.rb', line 241
def inspect
"#<#{self.class} ...>"
end
|
#key?(key) ⇒ Boolean
209
210
211
|
# File 'lib/graphql/query/context.rb', line 209
def key?(key)
@scoped_context.key?(key) || @provided_values.key?(key)
end
|
#logger ⇒ Object
237
238
239
|
# File 'lib/graphql/query/context.rb', line 237
def logger
@query && @query.logger
end
|
#namespace(ns) ⇒ Hash
Get an isolated hash for ns. Doesn't affect user-provided storage.
224
225
226
227
228
229
230
|
# File 'lib/graphql/query/context.rb', line 224
def namespace(ns)
if ns == :interpreter
self
else
@storage[ns]
end
end
|
#namespace?(ns) ⇒ Boolean
233
234
235
|
# File 'lib/graphql/query/context.rb', line 233
def namespace?(ns)
@storage.key?(ns)
end
|
#response_extensions ⇒ Hash
Modify this hash to return extensions to client.
58
59
60
|
# File 'lib/graphql/query/context.rb', line 58
def response_extensions
namespace(:__query_result_extensions__)
end
|
Use this when you need to do a scoped set inside a lazy-loaded (or batch-loaded)
block of code.
264
265
266
|
# File 'lib/graphql/query/context.rb', line 264
def scoped
Scoped.new(@scoped_context, current_path)
end
|
#scoped_merge!(hash) ⇒ Object
245
246
247
|
# File 'lib/graphql/query/context.rb', line 245
def scoped_merge!(hash)
@scoped_context.merge!(hash)
end
|
#scoped_set!(key, value) ⇒ Object
249
250
251
252
|
# File 'lib/graphql/query/context.rb', line 249
def scoped_set!(key, value)
scoped_merge!(key => value)
nil
end
|
#skip ⇒ Object
Return this value to tell the runtime
to exclude this field from the response altogether
113
114
115
|
# File 'lib/graphql/query/context.rb', line 113
def skip
GraphQL::Execution::SKIP
end
|
#to_h ⇒ Object
Also known as:
to_hash
199
200
201
202
203
204
205
|
# File 'lib/graphql/query/context.rb', line 199
def to_h
if (current_scoped_context = @scoped_context.merged_context)
@provided_values.merge(current_scoped_context)
else
@provided_values
end
end
|