class GraphQL::Query::Context
Expose some query-specific info to field resolve functions. It delegates [] to the hash that’s passed to GraphQL::Query#initialize.
Constants
- RUNTIME_METADATA_KEYS
- UNSPECIFIED_FETCH_DEFAULT
Attributes
Returns
-
Array<GraphQL::ExecutionError>— errors returned during execution
Returns
-
GraphQL::Query— The query whose context this is
Returns
Public Class Methods
# File lib/graphql/query/context.rb, line 59 def initialize(query:, schema: query.schema, values:) @query = query @schema = 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 = [] @scoped_context = ScopedContext.new(self) end
Make a new context which delegates key lookup to values
Parameters
-
query(GraphQL::Query) — the query who owns this context -
values(Hash) — A hash of arbitrary values which will be accessible at query-time
Public Instance Methods
Source
# File lib/graphql/query/context.rb, line 108 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 # not found nil end end
Lookup key from the hash passed to Schema.execute as context:
Source
# File lib/graphql/query/context.rb, line 92 def []=(key, value) @provided_values[key] = value end
# File lib/graphql/query/context.rb, line 145 def add_error(error) if !error.is_a?(GraphQL::RuntimeError) raise TypeError, "expected error to be a GraphQL::RuntimeError, but was #{error.class}" end errors << error nil end
Add error at query-level.
Parameters
-
error(GraphQL::ExecutionError) — an execution error
Returns
-
void
# File lib/graphql/query/context.rb, line 181 def backtrace GraphQL::Backtrace.new(self) end
Examples
Example: Print the GraphQL backtrace during field resolution
puts ctx.backtrace
Returns
-
GraphQL::Backtrace— The backtrace for this point in query execution
Source
# File lib/graphql/query/context.rb, line 189 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
Source
# File lib/graphql/query/context.rb, line 82 def dataloader @dataloader ||= self[:dataloader] || (query.multiplex ? query.multiplex.dataloader : schema.dataloader_class.new) end
Source
# File lib/graphql/query/context.rb, line 203 def delete(key) if @scoped_context.key?(key) @scoped_context.delete(key) else @provided_values.delete(key) end end
Source
# File lib/graphql/query/context.rb, line 231 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
Source
# File lib/graphql/query/context.rb, line 185 def execution_errors @execution_errors ||= ExecutionErrors.new(self) end
# File lib/graphql/query/context.rb, line 213 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
Source
# File lib/graphql/query/context.rb, line 308 def inspect "#<#{self.class} ...>" end
Source
# File lib/graphql/query/context.rb, line 258 def key?(key) @scoped_context.key?(key) || @provided_values.key?(key) end
Source
# File lib/graphql/query/context.rb, line 304 def logger @query && @query.logger end
Source
# File lib/graphql/query/context.rb, line 286 def namespace(ns) if ns == :interpreter self else @storage[ns] end end
Get an isolated hash for ns. Doesn’t affect user-provided storage.
Parameters
-
ns(Object) — a usage-specific namespace identifier
Returns
-
Hash— namespaced storage
Source
# File lib/graphql/query/context.rb, line 300 def namespace?(ns) @storage.key?(ns) end
Returns
-
Boolean— true if this namespace was accessed before
# File lib/graphql/query/context.rb, line 163 def raw_value(value) GraphQL::Execution::Interpreter::RawValue.new(value) end
Parameters
-
value(Object) — Any object to be inserted directly into the final response
Returns
-
GraphQL::Execution::Interpreter::RawValue— Return this from the field
Source
# File lib/graphql/query/context.rb, line 78 def response_extensions namespace(:__query_result_extensions__) end
Modify this hash to return extensions to client.
Returns
-
Hash— A hash that will be added verbatim to the result hash, as"extensions" => { ... }
Source
# File lib/graphql/query/context.rb, line 342 def scoped Scoped.new(@scoped_context, current_path) end
Use this when you need to do a scoped set inside a lazy-loaded (or batch-loaded) block of code.
Examples
Example: using scoped context inside a promise
scoped_ctx = context.scoped SomeBatchLoader.load(...).then do |thing| # use a scoped_ctx which was created _before_ dataloading: scoped_ctx.set!(:thing, thing) end
Returns
Source
# File lib/graphql/query/context.rb, line 312 def scoped_merge!(hash) @scoped_context.merge!(hash) end
Source
# File lib/graphql/query/context.rb, line 316 def scoped_set!(key, value) scoped_merge!(key => value) nil end
Source
# File lib/graphql/query/context.rb, line 129 def skip GraphQL::Execution::Skip.new end
Return this value to tell the runtime to exclude this field from the response altogether
Source
# File lib/graphql/query/context.rb, line 248 def to_h if (current_scoped_context = @scoped_context.merged_context) @provided_values.merge(current_scoped_context) else @provided_values end end
# File lib/graphql/query/context.rb, line 268 def warden @warden ||= (@query && @query.warden) end
Returns