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.



94
95
96
97
98
# File 'lib/graphql/query/context.rb', line 94

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

Instance Method Details

#[](key) ⇒ Object



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

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

#current_pathObject



117
118
119
120
# File 'lib/graphql/query/context.rb', line 117

def current_path
  thread_info = Thread.current[:__graphql_runtime_info]
  (thread_info && thread_info[:current_path]) || @no_path
end

#dig(key, *other_keys) ⇒ Object



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

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)


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

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

#merge!(hash) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/graphql/query/context.rb', line 108

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



100
101
102
103
104
105
106
# 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