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

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

Constant Summary collapse

NO_PATH =
[].freeze
NO_CONTEXT =
{}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(query_context) ⇒ ScopedContext

Returns a new instance of ScopedContext.



97
98
99
100
101
# File 'lib/graphql/query/context.rb', line 97

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

Instance Method Details

#[](key) ⇒ Object



137
138
139
140
141
142
143
144
# File 'lib/graphql/query/context.rb', line 137

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

#current_pathObject



146
147
148
# File 'lib/graphql/query/context.rb', line 146

def current_path
  @query_context.current_path || NO_PATH
end

#dig(key, *other_keys) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/graphql/query/context.rb', line 150

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)


126
127
128
129
130
131
132
133
134
135
# File 'lib/graphql/query/context.rb', line 126

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



115
116
117
118
119
120
121
122
123
124
# File 'lib/graphql/query/context.rb', line 115

def merge!(hash)
  @all_keys ||= Set.new
  @all_keys.merge(hash.keys)
  ctx = @scoped_contexts ||= {}
  current_path.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



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/graphql/query/context.rb', line 103

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