Class: GraphQL::Query::Context

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
SharedMethods
Defined in:
lib/graphql/query/context.rb

Overview

Expose some query-specific info to field resolve functions. It delegates [] to the hash that’s passed to GraphQL::Query#initialize.

Defined Under Namespace

Modules: SharedMethods Classes: ExecutionErrors, FieldResolutionContext

Constant Summary collapse

UNSPECIFIED_FETCH_DEFAULT =
Object.new

Instance Attribute Summary collapse

Attributes included from SharedMethods

#object, #skipped, #value

Instance Method Summary collapse

Methods included from SharedMethods

#add_error, #backtrace, #delete_child, #execution_errors, #invalid_null?, #lookahead, #skip, #spawn_child

Constructor Details

#initialize(query:, schema: query.schema, values:, object:) ⇒ Context

Make a new context which delegates key lookup to values

Parameters:

  • query (GraphQL::Query)

    the query who owns this context

  • values (Hash)

    A hash of arbitrary values which will be accessible at query-time



144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/graphql/query/context.rb', line 144

def initialize(query:, schema: query.schema, values:, object:)
  @query = query
  @schema = schema
  @provided_values = values || {}
  @object = object
  # Namespaced storage, where user-provided values are in `nil` namespace:
  @storage = Hash.new { |h, k| h[k] = {} }
  @storage[nil] = @provided_values
  @errors = []
  @path = []
  @value = nil
  @context = self # for SharedMethods
  @scoped_context = {}
end

Instance Attribute Details

#errorsArray<GraphQL::ExecutionError> (readonly)

Returns errors returned during execution.

Returns:



130
131
132
# File 'lib/graphql/query/context.rb', line 130

def errors
  @errors
end

#execution_strategyObject Also known as: strategy

Returns the value of attribute execution_strategy.



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

def execution_strategy
  @execution_strategy
end

#interpreter=(value) ⇒ Object (writeonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



164
165
166
# File 'lib/graphql/query/context.rb', line 164

def interpreter=(value)
  @interpreter = value
end

#pathArray<String, Integer> (readonly)

Returns The current position in the result.

Returns:

  • (Array<String, Integer>)

    The current position in the result



139
140
141
# File 'lib/graphql/query/context.rb', line 139

def path
  @path
end

#queryGraphQL::Query (readonly)

Returns The query whose context this is.

Returns:



133
134
135
# File 'lib/graphql/query/context.rb', line 133

def query
  @query
end

#schemaGraphQL::Schema (readonly)

Returns:



136
137
138
# File 'lib/graphql/query/context.rb', line 136

def schema
  @schema
end

#scoped_contextObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



170
171
172
# File 'lib/graphql/query/context.rb', line 170

def scoped_context
  @scoped_context
end

#value=(value) ⇒ Object (writeonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



167
168
169
# File 'lib/graphql/query/context.rb', line 167

def value=(value)
  @value = value
end

Instance Method Details

#[](key) ⇒ Object

Lookup key from the hash passed to Schema#execute as context:



182
183
184
185
# File 'lib/graphql/query/context.rb', line 182

def [](key)
  return @scoped_context[key] if @scoped_context.key?(key)
  @provided_values[key]
end

#[]=(key, value) ⇒ Object

Reassign key to the hash passed to Schema#execute as context:



178
179
180
# File 'lib/graphql/query/context.rb', line 178

def []=(key, value)
  @provided_values[key] = value
end

#ast_nodeGraphQL::Language::Nodes::Field

Returns The AST node for the currently-executing field.

Returns:



125
126
127
# File 'lib/graphql/query/context.rb', line 125

def ast_node
  @irep_node.ast_node
end

#dataloaderObject



159
160
161
# File 'lib/graphql/query/context.rb', line 159

def dataloader
  @dataloader ||= query.multiplex ? query.multiplex.dataloader : schema.dataloader_class.new
end

#delete(key) ⇒ Object



187
188
189
190
191
192
193
# File 'lib/graphql/query/context.rb', line 187

def delete(key)
  if @scoped_context.key?(key)
    @scoped_context.delete(key)
  else
    @provided_values.delete(key)
  end
end

#dig(key, *other_keys) ⇒ Object



211
212
213
# File 'lib/graphql/query/context.rb', line 211

def dig(key, *other_keys)
  @scoped_context.key?(key) ? @scoped_context.dig(key, *other_keys) : @provided_values.dig(key, *other_keys)
end

#fetch(key, default = UNSPECIFIED_FETCH_DEFAULT) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/graphql/query/context.rb', line 197

def fetch(key, default = UNSPECIFIED_FETCH_DEFAULT)
  if @scoped_context.key?(key)
    @scoped_context[key]
  elsif @provided_values.key?(key)
    @provided_values[key]
  elsif default != UNSPECIFIED_FETCH_DEFAULT
    default
  elsif block_given?
    yield(self, key)
  else
    raise KeyError.new(key: key)
  end
end

#inspectObject



236
237
238
# File 'lib/graphql/query/context.rb', line 236

def inspect
  "#<Query::Context ...>"
end

#irep_nodeGraphQL::InternalRepresentation::Node

Returns The internal representation for this query node.

Returns:



120
121
122
# File 'lib/graphql/query/context.rb', line 120

def irep_node
  @irep_node ||= query.irep_selection
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


220
221
222
# File 'lib/graphql/query/context.rb', line 220

def key?(key)
  @scoped_context.key?(key) || @provided_values.key?(key)
end

#namespace(ns) ⇒ Hash

Get an isolated hash for ns. Doesn’t affect user-provided storage.

Parameters:

  • ns (Object)

    a usage-specific namespace identifier

Returns:

  • (Hash)

    namespaced storage



232
233
234
# File 'lib/graphql/query/context.rb', line 232

def namespace(ns)
  @storage[ns]
end

#received_null_childObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



241
242
243
244
# File 'lib/graphql/query/context.rb', line 241

def received_null_child
  @invalid_null = true
  @value = nil
end

#scoped_merge!(hash) ⇒ Object



246
247
248
# File 'lib/graphql/query/context.rb', line 246

def scoped_merge!(hash)
  @scoped_context = @scoped_context.merge(hash)
end

#scoped_set!(key, value) ⇒ Object



250
251
252
253
# File 'lib/graphql/query/context.rb', line 250

def scoped_set!(key, value)
  scoped_merge!(key => value)
  nil
end

#to_hObject Also known as: to_hash



215
216
217
# File 'lib/graphql/query/context.rb', line 215

def to_h
  @provided_values.merge(@scoped_context)
end

#wardenGraphQL::Schema::Warden



225
226
227
# File 'lib/graphql/query/context.rb', line 225

def warden
  @warden ||= @query.warden
end