class GraphQL::Query::Context

It delegates ‘[]` to the hash that’s passed to ‘GraphQL::Query#initialize`.
Expose some query-specific info to field resolve functions.

def [](key)

Lookup `key` from the hash passed to {Schema#execute} as `context:`
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 = Thread.current[:__graphql_runtime_info]) &&
        (query_runtime_state = current_runtime_state[@query]) &&
        (query_runtime_state.public_send(key))
    end
  else
    # not found
    nil
  end
end

def []=(key, value)

def []=(key, value)
  @provided_values[key] = value
end

def current_path

def current_path
  current_runtime_state = Thread.current[:__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

def dataloader

def dataloader
  @dataloader ||= self[:dataloader] || (query.multiplex ? query.multiplex.dataloader : schema.dataloader_class.new)
end

def delete(key)

def delete(key)
  if @scoped_context.key?(key)
    @scoped_context.delete(key)
  else
    @provided_values.delete(key)
  end
end

def dig(key, *other_keys)

def dig(key, *other_keys)
  if RUNTIME_METADATA_KEYS.include?(key)
    (current_runtime_state = Thread.current[:__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

def fetch(key, default = UNSPECIFIED_FETCH_DEFAULT)

def fetch(key, default = UNSPECIFIED_FETCH_DEFAULT)
  if RUNTIME_METADATA_KEYS.include?(key)
    (runtime = Thread.current[:__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

def initialize(query:, schema: query.schema, values:, object:)

Parameters:
  • values (Hash) -- A hash of arbitrary values which will be accessible at query-time
  • query (GraphQL::Query) -- the query who owns this context
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

def inspect

def inspect
  "#<Query::Context ...>"
end

def key?(key)

def key?(key)
  @scoped_context.key?(key) || @provided_values.key?(key)
end

def namespace(ns)

Returns:
  • (Hash) - namespaced storage

Parameters:
  • ns (Object) -- a usage-specific namespace identifier
def namespace(ns)
  if ns == :interpreter
    self
  else
    @storage[ns]
  end
end

def namespace?(ns)

Returns:
  • (Boolean) - true if this namespace was accessed before
def namespace?(ns)
  @storage.key?(ns)
end

def response_extensions

Returns:
  • (Hash) - A hash that will be added verbatim to the result hash, as `"extensions" => { ... }`
def response_extensions
  namespace(:__query_result_extensions__)
end

def scoped_merge!(hash)

def scoped_merge!(hash)
  @scoped_context.merge!(hash)
end

def scoped_set!(key, value)

def scoped_set!(key, value)
  scoped_merge!(key => value)
  nil
end

def to_h

def to_h
  if (current_scoped_context = @scoped_context.merged_context)
    @provided_values.merge(current_scoped_context)
  else
    @provided_values
  end
end

def warden

Returns:
  • (GraphQL::Schema::Warden) -
def warden
  @warden ||= (@query && @query.warden)
end