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 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
Public Instance Methods
Lookup ‘key` from the hash passed to {Schema#execute} as `context:`
# File lib/graphql/query/context.rb, line 196 def [](key) if @scoped_context.key?(key) @scoped_context[key] else @provided_values[key] end end
# File lib/graphql/query/context.rb, line 186 def []=(key, value) @provided_values[key] = value end
# File lib/graphql/query/context.rb, line 173 def dataloader @dataloader ||= self[:dataloader] || (query.multiplex ? query.multiplex.dataloader : schema.dataloader_class.new) end
# File lib/graphql/query/context.rb, line 204 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 228 def dig(key, *other_keys) if @scoped_context.key?(key) @scoped_context.dig(key, *other_keys) else @provided_values.dig(key, *other_keys) end end
# File lib/graphql/query/context.rb, line 214 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 270 def inspect "#<Query::Context ...>" end
# File lib/graphql/query/context.rb, line 246 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 261 def namespace(ns) @storage[ns] end
@return [Boolean] true if this namespace was accessed before
# File lib/graphql/query/context.rb, line 266 def namespace?(ns) @storage.key?(ns) end
@return [Hash] A hash that will be added verbatim to the result hash, as ‘“extensions” => { … }`
# File lib/graphql/query/context.rb, line 169 def response_extensions namespace(:__query_result_extensions__) end
# File lib/graphql/query/context.rb, line 274 def scoped_merge!(hash) @scoped_context.merge!(hash) end
# File lib/graphql/query/context.rb, line 278 def scoped_set!(key, value) scoped_merge!(key => value) nil end
# File lib/graphql/query/context.rb, line 236 def to_h if (current_scoped_context = @scoped_context.merged_context) @provided_values.merge(current_scoped_context) else @provided_values end end
@return [GraphQL::Schema::Warden]
# File lib/graphql/query/context.rb, line 251 def warden @warden ||= (@query && @query.warden) end