Class: GraphQL::Query::Context
- Inherits:
-
Object
- Object
- GraphQL::Query::Context
- Extended by:
- Delegate
- 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
Classes: FieldResolutionContext
Instance Attribute Summary collapse
-
#errors ⇒ Array<GraphQL::ExecutionError>
readonly
Errors returned during execution.
-
#execution_strategy ⇒ Object
(also: #strategy)
Returns the value of attribute execution_strategy.
-
#irep_node ⇒ GraphQL::InternalRepresentation::Node
The internal representation for this query node.
-
#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
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:
. -
#add_error(error) ⇒ void
Add error at query-level.
-
#ast_node ⇒ GraphQL::Language::Nodes::Field
The AST node for the currently-executing field.
-
#initialize(query:, values:) ⇒ Context
constructor
Make a new context which delegates key lookup to
values
. -
#namespace(ns) ⇒ Hash
Get an isolated hash for
ns
. -
#skip ⇒ Object
Return this value to tell the runtime to exclude this field from the response altogether.
- #spawn(key:, selection:, parent_type:, field:) ⇒ Object
- #warden ⇒ GraphQL::Schema::Warden
Methods included from Delegate
Constructor Details
#initialize(query:, values:) ⇒ Context
Make a new context which delegates key lookup to values
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/graphql/query/context.rb', line 42 def initialize(query:, values:) @query = query @schema = query.schema @provided_values = values || {} # Namespaced storage, where user-provided values are in `nil` namespace: @storage = Hash.new { |h, k| h[k] = {} } @storage[nil] = @provided_values @errors = [] @path = [] end |
Instance Attribute Details
#errors ⇒ Array<GraphQL::ExecutionError> (readonly)
Returns errors returned during execution.
28 29 30 |
# File 'lib/graphql/query/context.rb', line 28 def errors @errors end |
#execution_strategy ⇒ Object Also known as: strategy
Returns the value of attribute execution_strategy.
8 9 10 |
# File 'lib/graphql/query/context.rb', line 8 def execution_strategy @execution_strategy end |
#irep_node ⇒ GraphQL::InternalRepresentation::Node
Returns The internal representation for this query node.
20 21 22 |
# File 'lib/graphql/query/context.rb', line 20 def irep_node @irep_node end |
#path ⇒ Array<String, Integer> (readonly)
Returns The current position in the result.
37 38 39 |
# File 'lib/graphql/query/context.rb', line 37 def path @path end |
#query ⇒ GraphQL::Query (readonly)
Returns The query whose context this is.
31 32 33 |
# File 'lib/graphql/query/context.rb', line 31 def query @query end |
#schema ⇒ GraphQL::Schema (readonly)
34 35 36 |
# File 'lib/graphql/query/context.rb', line 34 def schema @schema end |
Instance Method Details
#[](key) ⇒ Object
Lookup key
from the hash passed to Schema#execute as context:
|
# File 'lib/graphql/query/context.rb', line 55
|
#[]=(key, value) ⇒ Object
Reassign key
to the hash passed to Schema#execute as context:
|
# File 'lib/graphql/query/context.rb', line 58
|
#add_error(error) ⇒ void
This method returns an undefined value.
Add error at query-level.
93 94 95 96 97 98 99 |
# File 'lib/graphql/query/context.rb', line 93 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 |
#ast_node ⇒ GraphQL::Language::Nodes::Field
Returns The AST node for the currently-executing field.
23 24 25 |
# File 'lib/graphql/query/context.rb', line 23 def ast_node @irep_node.ast_node end |
#namespace(ns) ⇒ Hash
Get an isolated hash for ns
. Doesn't affect user-provided storage.
69 70 71 |
# File 'lib/graphql/query/context.rb', line 69 def namespace(ns) @storage[ns] end |
#skip ⇒ Object
Return this value to tell the runtime to exclude this field from the response altogether
86 87 88 |
# File 'lib/graphql/query/context.rb', line 86 def skip GraphQL::Execution::Execute::SKIP end |
#spawn(key:, selection:, parent_type:, field:) ⇒ Object
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/graphql/query/context.rb', line 73 def spawn(key:, selection:, parent_type:, field:) FieldResolutionContext.new( context: self, parent: self, key: key, selection: selection, parent_type: parent_type, field: field, ) end |
#warden ⇒ GraphQL::Schema::Warden
62 63 64 |
# File 'lib/graphql/query/context.rb', line 62 def warden @warden ||= @query.warden end |