Class: GraphQL::Query::Context::ScopedContext

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/query/context/scoped_context.rb

Instance Method Summary collapse

Constructor Details

#initialize(query_context) ⇒ ScopedContext

Returns a new instance of ScopedContext.



6
7
8
9
10
# File 'lib/graphql/query/context/scoped_context.rb', line 6

def initialize(query_context)
  @query_context = query_context
  @scoped_contexts = nil
  @all_keys = nil
end

Instance Method Details

#[](key) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/graphql/query/context/scoped_context.rb', line 46

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

#current_pathObject



55
56
57
# File 'lib/graphql/query/context/scoped_context.rb', line 55

def current_path
  @query_context.current_path || GraphQL::EmptyObjects::EMPTY_ARRAY
end

#dig(key, *other_keys) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/graphql/query/context/scoped_context.rb', line 59

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) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
# File 'lib/graphql/query/context/scoped_context.rb', line 35

def key?(key)
  if @all_keys && @all_keys.include?(key)
    each_present_path_ctx do |path_ctx|
      if path_ctx.key?(key)
        return true
      end
    end
  end
  false
end

#merge!(hash, at: current_path) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/graphql/query/context/scoped_context.rb', line 24

def merge!(hash, at: current_path)
  @all_keys ||= Set.new
  @all_keys.merge(hash.keys)
  ctx = @scoped_contexts ||= {}
  at.each do |path_part|
    ctx = ctx[path_part] ||= { parent: ctx }
  end
  this_scoped_ctx = ctx[:scoped_context] ||= {}
  this_scoped_ctx.merge!(hash)
end

#merged_contextObject



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/graphql/query/context/scoped_context.rb', line 12

def merged_context
  if @scoped_contexts.nil?
    GraphQL::EmptyObjects::EMPTY_HASH
  else
    merged_ctx = {}
    each_present_path_ctx do |path_ctx|
      merged_ctx = path_ctx.merge(merged_ctx)
    end
    merged_ctx
  end
end