Class: GraphQL::Query::Context
- Inherits:
-
Object
- Object
- GraphQL::Query::Context
- Extended by:
- Forwardable
- Includes:
- SharedMethods
- 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
.
Defined Under Namespace
Modules: SharedMethods Classes: ExecutionErrors, Scoped, ScopedContext
Constant Summary collapse
- RUNTIME_METADATA_KEYS =
Set.new([:current_object, :current_arguments, :current_field, :current_path])
- UNSPECIFIED_FETCH_DEFAULT =
Object.new
Instance Attribute Summary collapse
-
#errors ⇒ Array<GraphQL::ExecutionError>
readonly
Errors returned during execution.
-
#interpreter ⇒ Object
writeonly
private
-
#path ⇒ Array<String, Integer>
readonly
The current position in the result.
-
#query ⇒ GraphQL::Query
readonly
The query whose context this is.
-
#schema ⇒ GraphQL::Schema
readonly
-
#scoped_context ⇒ Object
readonly
private
-
#value ⇒ Object
writeonly
private
-
#warden ⇒ GraphQL::Schema::Warden
Instance Method Summary collapse
-
#[](key) ⇒ Object
Lookup
key
from the hash passed to Schema#execute ascontext:
. -
#[]=(key, value) ⇒ Object
Reassign
key
to the hash passed to Schema#execute ascontext:
. -
#current_path ⇒ Object
-
#dataloader ⇒ Object
-
#delete(key) ⇒ Object
-
#dig(key, *other_keys) ⇒ Object
-
#fetch(key, default = UNSPECIFIED_FETCH_DEFAULT) ⇒ Object
-
#initialize(query:, schema: query.schema, values:, object:) ⇒ Context
constructor
Make a new context which delegates key lookup to
values
. -
#inspect ⇒ Object
-
#key?(key) ⇒ Boolean
-
#logger ⇒ Object
-
#namespace(ns) ⇒ Hash
Get an isolated hash for
ns
. -
#namespace?(ns) ⇒ Boolean
True if this namespace was accessed before.
-
#response_extensions ⇒ Hash
A hash that will be added verbatim to the result hash, as
"extensions" => { ... }
. -
#scoped ⇒ Context::Scoped
Use this when you need to do a scoped set inside a lazy-loaded (or batch-loaded) block of code.
-
#scoped_merge!(hash) ⇒ Object
-
#scoped_set!(key, value) ⇒ Object
-
#to_h ⇒ Object
(also: #to_hash)
Methods included from SharedMethods
#add_error, #backtrace, #execution_errors, #skip
Constructor Details
#initialize(query:, schema: query.schema, values:, object:) ⇒ Context
Make a new context which delegates key lookup to values
80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/graphql/query/context.rb', line 80 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 TODO delete sharedmethods @scoped_context = ScopedContext.new(self) end |
Instance Attribute Details
#errors ⇒ Array<GraphQL::ExecutionError> (readonly)
Returns errors returned during execution.
66 67 68 |
# File 'lib/graphql/query/context.rb', line 66 def errors @errors 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.
105 106 107 |
# File 'lib/graphql/query/context.rb', line 105 def interpreter=(value) @interpreter = value end |
#path ⇒ Array<String, Integer> (readonly)
Returns The current position in the result.
75 76 77 |
# File 'lib/graphql/query/context.rb', line 75 def path @path end |
#query ⇒ GraphQL::Query (readonly)
Returns The query whose context this is.
69 70 71 |
# File 'lib/graphql/query/context.rb', line 69 def query @query end |
#schema ⇒ GraphQL::Schema (readonly)
72 73 74 |
# File 'lib/graphql/query/context.rb', line 72 def schema @schema end |
#scoped_context ⇒ Object (readonly)
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.
111 112 113 |
# File 'lib/graphql/query/context.rb', line 111 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.
108 109 110 |
# File 'lib/graphql/query/context.rb', line 108 def value=(value) @value = value end |
#warden ⇒ GraphQL::Schema::Warden
217 218 219 |
# File 'lib/graphql/query/context.rb', line 217 def warden @warden ||= (@query && @query.warden) end |
Instance Method Details
#[](key) ⇒ Object
Lookup key
from the hash passed to Schema#execute as context:
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/graphql/query/context.rb', line 124 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 = Thread.current[:__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
Reassign key
to the hash passed to Schema#execute as context:
120 121 122 |
# File 'lib/graphql/query/context.rb', line 120 def []=(key, value) @provided_values[key] = value end |
#current_path ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/graphql/query/context.rb', line 143 def current_path current_runtime_state = Thread.current[:__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
100 101 102 |
# File 'lib/graphql/query/context.rb', line 100 def dataloader @dataloader ||= self[:dataloader] || (query.multiplex ? query.multiplex.dataloader : schema.dataloader_class.new) end |
#delete(key) ⇒ Object
157 158 159 160 161 162 163 |
# File 'lib/graphql/query/context.rb', line 157 def delete(key) if @scoped_context.key?(key) @scoped_context.delete(key) else @provided_values.delete(key) end end |
#dig(key, *other_keys) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/graphql/query/context.rb', line 185 def dig(key, *other_keys) if RUNTIME_METADATA_KEYS.include?(key) (current_runtime_state = Thread.current[:__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 |
#fetch(key, default = UNSPECIFIED_FETCH_DEFAULT) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/graphql/query/context.rb', line 167 def fetch(key, default = UNSPECIFIED_FETCH_DEFAULT) if RUNTIME_METADATA_KEYS.include?(key) (runtime = Thread.current[:__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
244 245 246 |
# File 'lib/graphql/query/context.rb', line 244 def inspect "#<Query::Context ...>" end |
#key?(key) ⇒ Boolean
212 213 214 |
# File 'lib/graphql/query/context.rb', line 212 def key?(key) @scoped_context.key?(key) || @provided_values.key?(key) end |
#logger ⇒ Object
240 241 242 |
# File 'lib/graphql/query/context.rb', line 240 def logger @query && @query.logger end |
#namespace(ns) ⇒ Hash
Get an isolated hash for ns
. Doesn’t affect user-provided storage.
227 228 229 230 231 232 233 |
# File 'lib/graphql/query/context.rb', line 227 def namespace(ns) if ns == :interpreter self else @storage[ns] end end |
#namespace?(ns) ⇒ Boolean
Returns true if this namespace was accessed before.
236 237 238 |
# File 'lib/graphql/query/context.rb', line 236 def namespace?(ns) @storage.key?(ns) end |
#response_extensions ⇒ Hash
Returns A hash that will be added verbatim to the result hash, as "extensions" => { ... }
.
96 97 98 |
# File 'lib/graphql/query/context.rb', line 96 def response_extensions namespace(:__query_result_extensions__) end |
#scoped ⇒ Context::Scoped
Use this when you need to do a scoped set inside a lazy-loaded (or batch-loaded) block of code.
267 268 269 |
# File 'lib/graphql/query/context.rb', line 267 def scoped Scoped.new(@scoped_context, current_path) end |
#scoped_merge!(hash) ⇒ Object
248 249 250 |
# File 'lib/graphql/query/context.rb', line 248 def scoped_merge!(hash) @scoped_context.merge!(hash) end |
#scoped_set!(key, value) ⇒ Object
252 253 254 255 |
# File 'lib/graphql/query/context.rb', line 252 def scoped_set!(key, value) scoped_merge!(key => value) nil end |
#to_h ⇒ Object Also known as: to_hash
202 203 204 205 206 207 208 |
# File 'lib/graphql/query/context.rb', line 202 def to_h if (current_scoped_context = @scoped_context.merged_context) @provided_values.merge(current_scoped_context) else @provided_values end end |