Class: GraphQL::Query::Context
- Inherits:
-
Object
- Object
- GraphQL::Query::Context
- Extended by:
- Forwardable
- Includes:
- SharedMethods
- Defined in:
- lib/graphql/query/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
Modules: SharedMethods Classes: ExecutionErrors, 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
-
#errors ⇒ Array<GraphQL::ExecutionError>
readonly
Errors returned during execution.
-
#interpreter ⇒ Object
writeonly
private
-
#path ⇒ Array<String, Integer>
readonly
The current position in the result.
-
#query ⇒ GraphQL::Query
readonly
The query whose context this is.
-
#schema ⇒ GraphQL::Schema
readonly
-
#scoped_context ⇒ Object
readonly
private
-
#value ⇒ Object
writeonly
private
-
#warden ⇒ GraphQL::Schema::Warden
Instance Method Summary collapse
-
#[](key) ⇒ Object
Lookup
key
from the hash passed to Schema#execute ascontext:
. -
#[]=(key, value) ⇒ Object
Reassign
key
to the hash passed to Schema#execute ascontext:
. -
#current_path ⇒ Object
-
#dataloader ⇒ Object
-
#delete(key) ⇒ Object
-
#dig(key, *other_keys) ⇒ Object
-
#fetch(key, default = UNSPECIFIED_FETCH_DEFAULT) ⇒ Object
-
#initialize(query:, schema: query.schema, values:, object:) ⇒ Context
constructor
Make a new context which delegates key lookup to
values
. -
#inspect ⇒ Object
-
#key?(key) ⇒ Boolean
-
#namespace(ns) ⇒ Hash
Get an isolated hash for
ns
. -
#namespace?(ns) ⇒ Boolean
True if this namespace was accessed before.
-
#response_extensions ⇒ Hash
A hash that will be added verbatim to the result hash, as
"extensions" => { ... }
. -
#scoped_merge!(hash) ⇒ Object
-
#scoped_set!(key, value) ⇒ Object
-
#to_h ⇒ Object
(also: #to_hash)
Methods included from SharedMethods
#add_error, #backtrace, #execution_errors, #skip
Constructor Details
#initialize(query:, schema: query.schema, values:, object:) ⇒ Context
Make a new context which delegates key lookup to values
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/graphql/query/context.rb', line 78 def initialize(query:, schema: query.schema, values:, object:) @query = query @schema = schema @provided_values = values || {} @object = object # Namespaced storage, where user-provided values are in `nil` namespace: @storage = Hash.new { |h, k| h[k] = {} } @storage[nil] = @provided_values @errors = [] @path = [] @value = nil @context = self # for SharedMethods TODO delete sharedmethods @scoped_context = ScopedContext.new(self) end |
Instance Attribute Details
#errors ⇒ Array<GraphQL::ExecutionError> (readonly)
Returns errors returned during execution.
64 65 66 |
# File 'lib/graphql/query/context.rb', line 64 def errors @errors end |
#interpreter=(value) ⇒ Object (writeonly)
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.
201 202 203 |
# File 'lib/graphql/query/context.rb', line 201 def interpreter=(value) @interpreter = value end |
#path ⇒ Array<String, Integer> (readonly)
Returns The current position in the result.
73 74 75 |
# File 'lib/graphql/query/context.rb', line 73 def path @path end |
#query ⇒ GraphQL::Query (readonly)
Returns The query whose context this is.
67 68 69 |
# File 'lib/graphql/query/context.rb', line 67 def query @query end |
#schema ⇒ GraphQL::Schema (readonly)
70 71 72 |
# File 'lib/graphql/query/context.rb', line 70 def schema @schema end |
#scoped_context ⇒ Object (readonly)
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.
207 208 209 |
# File 'lib/graphql/query/context.rb', line 207 def scoped_context @scoped_context end |
#value=(value) ⇒ Object (writeonly)
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.
204 205 206 |
# File 'lib/graphql/query/context.rb', line 204 def value=(value) @value = value end |
#warden ⇒ GraphQL::Schema::Warden
313 314 315 |
# File 'lib/graphql/query/context.rb', line 313 def warden @warden ||= (@query && @query.warden) end |
Instance Method Details
#[](key) ⇒ Object
Lookup key
from the hash passed to Schema#execute as context:
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/graphql/query/context.rb', line 220 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 = Thread.current[:__graphql_runtime_info]) && (query_runtime_state = current_runtime_state[@query]) && (query_runtime_state.public_send(key)) end else # not found nil end end |
#[]=(key, value) ⇒ Object
Reassign key
to the hash passed to Schema#execute as context:
216 217 218 |
# File 'lib/graphql/query/context.rb', line 216 def []=(key, value) @provided_values[key] = value end |
#current_path ⇒ Object
239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/graphql/query/context.rb', line 239 def current_path current_runtime_state = Thread.current[:__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
196 197 198 |
# File 'lib/graphql/query/context.rb', line 196 def dataloader @dataloader ||= self[:dataloader] || (query.multiplex ? query.multiplex.dataloader : schema.dataloader_class.new) end |
#delete(key) ⇒ Object
253 254 255 256 257 258 259 |
# File 'lib/graphql/query/context.rb', line 253 def delete(key) if @scoped_context.key?(key) @scoped_context.delete(key) else @provided_values.delete(key) end end |
#dig(key, *other_keys) ⇒ Object
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/graphql/query/context.rb', line 281 def dig(key, *other_keys) if RUNTIME_METADATA_KEYS.include?(key) (current_runtime_state = Thread.current[:__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 |
#fetch(key, default = UNSPECIFIED_FETCH_DEFAULT) ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/graphql/query/context.rb', line 263 def fetch(key, default = UNSPECIFIED_FETCH_DEFAULT) if RUNTIME_METADATA_KEYS.include?(key) (runtime = Thread.current[:__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
336 337 338 |
# File 'lib/graphql/query/context.rb', line 336 def inspect "#<Query::Context ...>" end |
#key?(key) ⇒ Boolean
308 309 310 |
# File 'lib/graphql/query/context.rb', line 308 def key?(key) @scoped_context.key?(key) || @provided_values.key?(key) end |
#namespace(ns) ⇒ Hash
Get an isolated hash for ns
. Doesn’t affect user-provided storage.
323 324 325 326 327 328 329 |
# File 'lib/graphql/query/context.rb', line 323 def namespace(ns) if ns == :interpreter self else @storage[ns] end end |
#namespace?(ns) ⇒ Boolean
Returns true if this namespace was accessed before.
332 333 334 |
# File 'lib/graphql/query/context.rb', line 332 def namespace?(ns) @storage.key?(ns) end |
#response_extensions ⇒ Hash
Returns A hash that will be added verbatim to the result hash, as "extensions" => { ... }
.
192 193 194 |
# File 'lib/graphql/query/context.rb', line 192 def response_extensions namespace(:__query_result_extensions__) end |
#scoped_merge!(hash) ⇒ Object
340 341 342 |
# File 'lib/graphql/query/context.rb', line 340 def scoped_merge!(hash) @scoped_context.merge!(hash) end |
#scoped_set!(key, value) ⇒ Object
344 345 346 347 |
# File 'lib/graphql/query/context.rb', line 344 def scoped_set!(key, value) scoped_merge!(key => value) nil end |
#to_h ⇒ Object Also known as: to_hash
298 299 300 301 302 303 304 |
# File 'lib/graphql/query/context.rb', line 298 def to_h if (current_scoped_context = @scoped_context.merged_context) @provided_values.merge(current_scoped_context) else @provided_values end end |