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
- UNSPECIFIED_FETCH_DEFAULT
Attributes
@return [Array<GraphQL::ExecutionError>] errors returned during execution
@api private
@return [Array<String, Integer>] The current position in the result
@return [GraphQL::Query] The query whose context this is
@return [GraphQL::Schema]
@api private
@api private
@api private
Public Class Methods
Make a new context which delegates key lookup to `values` @param query [GraphQL::Query] the query who owns this context @param values [Hash] A hash of arbitrary values which will be accessible at query-time
# File lib/graphql/query/context.rb, line 144 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 @scoped_context = {} end
Public Instance Methods
Lookup `key` from the hash passed to {Schema#execute} as `context:`
# File lib/graphql/query/context.rb, line 187 def [](key) return @scoped_context[key] if @scoped_context.key?(key) @provided_values[key] end
# File lib/graphql/query/context.rb, line 177 def []=(key, value) @provided_values[key] = value end
@return [GraphQL::Language::Nodes::Field] The AST node for the currently-executing field
# File lib/graphql/query/context.rb, line 125 def ast_node @irep_node.ast_node end
# File lib/graphql/query/context.rb, line 164 def dataloader @dataloader ||= self[:dataloader] || (query.multiplex ? query.multiplex.dataloader : schema.dataloader_class.new) end
# File lib/graphql/query/context.rb, line 192 def delete(key) if @scoped_context.key?(key) @scoped_context.delete(key) else @provided_values.delete(key) end end
# File lib/graphql/query/context.rb, line 216 def dig(key, *other_keys) @scoped_context.key?(key) ? @scoped_context.dig(key, *other_keys) : @provided_values.dig(key, *other_keys) end
# File lib/graphql/query/context.rb, line 112 def execution_strategy=(new_strategy) # GraphQL::Batch re-assigns this value but it was previously not used # (ExecutionContext#strategy was used instead) # now it _is_ used, but it breaks GraphQL::Batch tests @execution_strategy ||= new_strategy end
# File lib/graphql/query/context.rb, line 202 def fetch(key, default = UNSPECIFIED_FETCH_DEFAULT) if @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
# File lib/graphql/query/context.rb, line 249 def inspect "#<Query::Context ...>" end
@return [GraphQL::InternalRepresentation::Node] The internal representation for this query node
# File lib/graphql/query/context.rb, line 120 def irep_node @irep_node ||= query.irep_selection end
# File lib/graphql/query/context.rb, line 225 def key?(key) @scoped_context.key?(key) || @provided_values.key?(key) end
Get an isolated hash for `ns`. Doesn't affect user-provided storage. @param ns [Object] a usage-specific namespace identifier @return [Hash] namespaced storage
# File lib/graphql/query/context.rb, line 240 def namespace(ns) @storage[ns] end
@return [Boolean] true if this namespace was accessed before
# File lib/graphql/query/context.rb, line 245 def namespace?(ns) @storage.key?(ns) end
@api private
# File lib/graphql/query/context.rb, line 254 def received_null_child @invalid_null = true @value = nil end
@return [Hash] A hash that will be added verbatim to the result hash, as `“extensions” => { … }`
# File lib/graphql/query/context.rb, line 160 def response_extensions namespace(:__query_result_extensions__) end
# File lib/graphql/query/context.rb, line 259 def scoped_merge!(hash) @scoped_context = @scoped_context.merge(hash) end
# File lib/graphql/query/context.rb, line 263 def scoped_set!(key, value) scoped_merge!(key => value) nil end
# File lib/graphql/query/context.rb, line 220 def to_h @provided_values.merge(@scoped_context) end
@return [GraphQL::Schema::Warden]
# File lib/graphql/query/context.rb, line 230 def warden @warden ||= (@query && @query.warden) end