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

errors[R]

@return [Array<GraphQL::ExecutionError>] errors returned during execution

interpreter[W]

@api private

path[R]

@return [Array<String, Integer>] The current position in the result

query[R]

@return [GraphQL::Query] The query whose context this is

schema[R]

@return [GraphQL::Schema]

scoped_context[R]

@api private

value[W]

@api private

warden[W]

@api private

Public Class Methods

new(query:, schema: query.schema, values:, object:) click to toggle source

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

[](key) click to toggle source

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
[]=(key, value) click to toggle source
# File lib/graphql/query/context.rb, line 186
def []=(key, value)
  @provided_values[key] = value
end
dataloader() click to toggle source
# File lib/graphql/query/context.rb, line 173
def dataloader
  @dataloader ||= self[:dataloader] || (query.multiplex ? query.multiplex.dataloader : schema.dataloader_class.new)
end
delete(key) click to toggle source
# 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
dig(key, *other_keys) click to toggle source
# 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
fetch(key, default = UNSPECIFIED_FETCH_DEFAULT) { |self, key| ... } click to toggle source
# 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
inspect() click to toggle source
# File lib/graphql/query/context.rb, line 270
def inspect
  "#<Query::Context ...>"
end
key?(key) click to toggle source
# File lib/graphql/query/context.rb, line 246
def key?(key)
  @scoped_context.key?(key) || @provided_values.key?(key)
end
namespace(ns) click to toggle source

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
namespace?(ns) click to toggle source

@return [Boolean] true if this namespace was accessed before

# File lib/graphql/query/context.rb, line 266
def namespace?(ns)
  @storage.key?(ns)
end
response_extensions() click to toggle source

@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
scoped_merge!(hash) click to toggle source
# File lib/graphql/query/context.rb, line 274
def scoped_merge!(hash)
  @scoped_context.merge!(hash)
end
scoped_set!(key, value) click to toggle source
# File lib/graphql/query/context.rb, line 278
def scoped_set!(key, value)
  scoped_merge!(key => value)
  nil
end
to_h() click to toggle source
# 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
Also aliased as: to_hash
to_hash()
Alias for: to_h
warden() click to toggle source

@return [GraphQL::Schema::Warden]

# File lib/graphql/query/context.rb, line 251
def warden
  @warden ||= (@query && @query.warden)
end