class GraphQL::Query::Context::ScopedContext

def [](key)

def [](key)
  each_present_path_ctx do |path_ctx|
    if path_ctx.key?(key)
      return path_ctx[key]
    end
  end
  nil
end

def current_path

def current_path
  @query_context.namespace(:interpreter)[:current_path] || @no_path
end

def dig(key, *other_keys)

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

def each_present_path_ctx

but look up the tree for previously-assigned scoped values
Start at the current location,
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

def initialize(query_context)

def initialize(query_context)
  @query_context = query_context
  @path_contexts = {}
  @no_path = [].freeze
end

def key?(key)

def key?(key)
  each_present_path_ctx do |path_ctx|
    if path_ctx.key?(key)
      return true
    end
  end
  false
end

def merge!(hash)

def merge!(hash)
  current_ctx = @path_contexts[current_path] ||= {}
  current_ctx.merge!(hash)
end

def merged_context

def merged_context
  merged_ctx = {}
  each_present_path_ctx do |path_ctx|
    merged_ctx = path_ctx.merge(merged_ctx)
  end
  merged_ctx
end