Class: GraphQL::Query::Context

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Schema::Member::HasDataloader
Defined in:
lib/graphql/query/context.rb,
lib/graphql/query/context/scoped_context.rb

Overview

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

Direct Known Subclasses

NullContext

Defined Under Namespace

Classes: ExecutionErrors, Scoped, ScopedContext

Constant Summary collapse

RUNTIME_METADATA_KEYS =
Set.new([:current_object, :current_arguments, :current_field, :current_path]).freeze
UNSPECIFIED_FETCH_DEFAULT =
Object.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Schema::Member::HasDataloader

#dataload, #dataload_all, #dataload_all_associations, #dataload_all_records, #dataload_association, #dataload_record

Constructor Details

#initialize(query:, schema: query.schema, values:) ⇒ 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

:call-seq:

initialize(GraphQL::Query query:, schema:, Hash values:)


60
61
62
63
64
65
66
67
68
69
# File 'lib/graphql/query/context.rb', line 60

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

Instance Attribute Details

#errors ⇒ Object (readonly)

Returns

  • Array<GraphQL::ExecutionError> — errors returned during execution


39
40
41
# File 'lib/graphql/query/context.rb', line 39

def errors
  @errors
end

#interpreter=(value) ⇒ Object (writeonly)

:nodoc:



87
88
89
# File 'lib/graphql/query/context.rb', line 87

def interpreter=(value)
  @interpreter = value
end

#query ⇒ Object (readonly)

Returns

  • GraphQL::Query — The query whose context this is


44
45
46
# File 'lib/graphql/query/context.rb', line 44

def query
  @query
end

#schema ⇒ Object (readonly)

Returns

  • GraphQL::Schema


49
50
51
# File 'lib/graphql/query/context.rb', line 49

def schema
  @schema
end

#scoped_context ⇒ Object (readonly)

:nodoc:



91
92
93
# File 'lib/graphql/query/context.rb', line 91

def scoped_context
  @scoped_context
end

#types ⇒ Object



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

def types
  @types ||= @query.types
end

#value=(value) ⇒ Object (writeonly)

:nodoc:



89
90
91
# File 'lib/graphql/query/context.rb', line 89

def value=(value)
  @value = value
end

#warden ⇒ Object

Returns

  • GraphQL::Schema::Warden

:call-seq:

warden() -> GraphQL::Schema::Warden


269
270
271
# File 'lib/graphql/query/context.rb', line 269

def warden
  @warden ||= (@query && @query.warden)
end

Instance Method Details

#[](key) ⇒ Object

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



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/graphql/query/context.rb', line 109

def [](key)
  if @scoped_context.key?(key)
    @scoped_context[key]
  elsif @provided_values.key?(key)
    @provided_values[key]
  elsif RUNTIME_METADATA_KEYS.include?(key)
    if key == :current_path
      current_path
    else
      (current_runtime_state = Fiber[:__graphql_runtime_info]) &&
        (query_runtime_state = current_runtime_state[@query]) &&
        (query_runtime_state.public_send(key))
    end
  else
    # not found
    nil
  end
end

#[]=(key, value) ⇒ Object



93
94
95
# File 'lib/graphql/query/context.rb', line 93

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

#add_error(error) ⇒ Object

Add error at query-level.

Parameters

  • error (GraphQL::ExecutionError) — an execution error

Returns

  • void

:call-seq:

add_error(GraphQL::ExecutionError error) -> void


146
147
148
149
150
151
152
# File 'lib/graphql/query/context.rb', line 146

def add_error(error)
  if !error.is_a?(GraphQL::RuntimeError)
    raise TypeError, "expected error to be a GraphQL::RuntimeError, but was #{error.class}"
  end
  errors << error
  nil
end

#backtrace ⇒ Object

Examples

Example: Print the GraphQL backtrace during field resolution

puts ctx.backtrace

Returns

  • GraphQL::Backtrace — The backtrace for this point in query execution

:call-seq:

backtrace() -> GraphQL::Backtrace


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

def backtrace
  GraphQL::Backtrace.new(self)
end

#current_path ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/graphql/query/context.rb', line 190

def current_path
  current_runtime_state = Fiber[:__graphql_runtime_info]
  query_runtime_state = current_runtime_state && current_runtime_state[@query]

  path = query_runtime_state &&
    (result = query_runtime_state.current_result) &&
    (result.path)
  if path && (rn = query_runtime_state.current_result_name)
    path = path.dup
    path.push(rn)
  end
  path
end

#dataloader ⇒ Object



83
84
85
# File 'lib/graphql/query/context.rb', line 83

def dataloader
  @dataloader ||= self[:dataloader] || (query.multiplex ? query.multiplex.dataloader : schema.dataloader_class.new)
end

#delete(key) ⇒ Object



204
205
206
207
208
209
210
# File 'lib/graphql/query/context.rb', line 204

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

#dig(key, *other_keys) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/graphql/query/context.rb', line 232

def dig(key, *other_keys)
  if RUNTIME_METADATA_KEYS.include?(key)
    (current_runtime_state = Fiber[:__graphql_runtime_info]) &&
      (query_runtime_state = current_runtime_state[@query]) &&
      (obj = query_runtime_state.public_send(key)) &&
      if other_keys.empty?
        obj
      else
        obj.dig(*other_keys)
      end
  elsif @scoped_context.key?(key)
    @scoped_context.dig(key, *other_keys)
  else
    @provided_values.dig(key, *other_keys)
  end
end

#execution_errors ⇒ Object



186
187
188
# File 'lib/graphql/query/context.rb', line 186

def execution_errors
  @execution_errors ||= ExecutionErrors.new(self)
end

#fetch(key, default = UNSPECIFIED_FETCH_DEFAULT) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/graphql/query/context.rb', line 214

def fetch(key, default = UNSPECIFIED_FETCH_DEFAULT)
  if RUNTIME_METADATA_KEYS.include?(key)
    (runtime = Fiber[:__graphql_runtime_info]) &&
      (query_runtime_state = runtime[@query]) &&
      (query_runtime_state.public_send(key))
  elsif @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

#inspect ⇒ Object



309
310
311
# File 'lib/graphql/query/context.rb', line 309

def inspect
  "#<#{self.class} ...>"
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


259
260
261
# File 'lib/graphql/query/context.rb', line 259

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

#logger ⇒ Object



305
306
307
# File 'lib/graphql/query/context.rb', line 305

def logger
  @query && @query.logger
end

#namespace(ns) ⇒ Object

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

Parameters

  • ns (Object) — a usage-specific namespace identifier

Returns

  • Hash — namespaced storage

:call-seq:

namespace(Object ns) -> Hash


287
288
289
290
291
292
293
# File 'lib/graphql/query/context.rb', line 287

def namespace(ns)
  if ns == :interpreter
    self
  else
    @storage[ns]
  end
end

#namespace?(ns) ⇒ Boolean

Returns

  • Boolean — true if this namespace was accessed before

:call-seq:

namespace?(ns) -> bool

Returns:

  • (Boolean)


301
302
303
# File 'lib/graphql/query/context.rb', line 301

def namespace?(ns)
  @storage.key?(ns)
end

#raw_value(value) ⇒ Object

Parameters

  • value (Object) — Any object to be inserted directly into the final response

Returns

  • GraphQL::Execution::Interpreter::RawValue — Return this from the field

:call-seq:

raw_value(Object value) -> GraphQL::Execution::Interpreter::RawValue


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

def raw_value(value)
  GraphQL::Execution::Interpreter::RawValue.new(value)
end

#response_extensions ⇒ Object

Modify this hash to return extensions to client.

Returns

  • Hash — A hash that will be added verbatim to the result hash, as "extensions" => { ... }

:call-seq:

response_extensions() -> Hash


79
80
81
# File 'lib/graphql/query/context.rb', line 79

def response_extensions
  namespace(:__query_result_extensions__)
end

#scoped ⇒ Object

Use this when you need to do a scoped set inside a lazy-loaded (or batch-loaded) block of code.

Examples

Example: using scoped context inside a promise

scoped_ctx = context.scoped
SomeBatchLoader.load(...).then do |thing|
  # use a scoped_ctx which was created _before_ dataloading:
  scoped_ctx.set!(:thing, thing)
end

Returns

  • Context::Scoped

:call-seq:

scoped() -> Context::Scoped


343
344
345
# File 'lib/graphql/query/context.rb', line 343

def scoped
  Scoped.new(@scoped_context, current_path)
end

#scoped_merge!(hash) ⇒ Object



313
314
315
# File 'lib/graphql/query/context.rb', line 313

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

#scoped_set!(key, value) ⇒ Object



317
318
319
320
# File 'lib/graphql/query/context.rb', line 317

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

#skip ⇒ Object

Return this value to tell the runtime to exclude this field from the response altogether



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

def skip
  GraphQL::Execution::Skip.new
end

#to_h ⇒ Object Also known as: to_hash



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

def to_h
  if (current_scoped_context = @scoped_context.merged_context)
    @provided_values.merge(current_scoped_context)
  else
    @provided_values
  end
end