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
  @path_contexts = {}
  @no_path = [].freeze
end

Instance Method Details

#[](key) ⇒ Object


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

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

#current_pathObject


113
114
115
# File 'lib/graphql/query/context.rb', line 113

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

#dig(key, *other_keys) ⇒ Object


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

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)

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

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
# File 'lib/graphql/query/context.rb', line 108

def merge!(hash)
  current_ctx = @path_contexts[current_path] ||= {}
  current_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