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

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

Instance Method Summary collapse

Constructor Details

#initialize(query_context) ⇒ ScopedContext

Returns a new instance of ScopedContext.



106
107
108
109
110
111
# File 'lib/graphql/query/context.rb', line 106

def initialize(query_context)
  @query_context = query_context
  @scoped_contexts = {}
  @all_keys = Set.new
  @no_path = [].freeze
end

Instance Method Details

#[](key) ⇒ Object



142
143
144
145
146
147
148
149
# File 'lib/graphql/query/context.rb', line 142

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

#current_pathObject



151
152
153
# File 'lib/graphql/query/context.rb', line 151

def current_path
  @query_context.current_path || @no_path
end

#dig(key, *other_keys) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/graphql/query/context.rb', line 155

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)


131
132
133
134
135
136
137
138
139
140
# File 'lib/graphql/query/context.rb', line 131

def key?(key)
  if @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



121
122
123
124
125
126
127
128
129
# File 'lib/graphql/query/context.rb', line 121

def merge!(hash)
  @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



113
114
115
116
117
118
119
# File 'lib/graphql/query/context.rb', line 113

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