class GraphQL::Query::Context::ScopedContext

Public Class Methods

new(query_context) click to toggle source
# File lib/graphql/query/context.rb, line 94
def initialize(query_context)
  @query_context = query_context
  @path_contexts = {}
  @no_path = [].freeze
end

Public Instance Methods

[](key) click to toggle source
# File lib/graphql/query/context.rb, line 126
def [](key)
  each_present_path_ctx do |path_ctx|
    if path_ctx.key?(key)
      return path_ctx[key]
    end
  end
  nil
end
current_path() click to toggle source
# File lib/graphql/query/context.rb, line 113
def current_path
  @query_context.namespace(:interpreter)[:current_path] || @no_path
end
dig(key, *other_keys) click to toggle source
# File lib/graphql/query/context.rb, line 135
def dig(key, *other_keys)
  each_present_path_ctx do |path_ctx|
    if path_ctx.key?(key)
      found_value = path_ctx[key]
      if other_keys.any?
        return found_value.dig(*other_keys)
      else
        return found_value
      end
    end
  end
  nil
end
key?(key) click to toggle source
# File lib/graphql/query/context.rb, line 117
def key?(key)
  each_present_path_ctx do |path_ctx|
    if path_ctx.key?(key)
      return true
    end
  end
  false
end
merge!(hash) click to toggle source
# File lib/graphql/query/context.rb, line 108
def merge!(hash)
  current_ctx = @path_contexts[current_path] ||= {}
  current_ctx.merge!(hash)
end
merged_context() click to toggle source
# File lib/graphql/query/context.rb, line 100
def merged_context
  merged_ctx = {}
  each_present_path_ctx do |path_ctx|
    merged_ctx = path_ctx.merge(merged_ctx)
  end
  merged_ctx
end

Private Instance Methods

each_present_path_ctx() { |current_path_ctx| ... } click to toggle source

Start at the current location, but look up the tree for previously-assigned scoped values

# File lib/graphql/query/context.rb, line 153
def each_present_path_ctx
  search_path = current_path.dup
  if (current_path_ctx = @path_contexts[search_path])
    yield(current_path_ctx)
  end

  while search_path.size > 0
    search_path.pop # look one level higher
    if (search_path_ctx = @path_contexts[search_path])
      yield(search_path_ctx)
    end
  end
end