Class: GraphQL::Execution::Interpreter::Runtime Private
- Inherits:
-
Object
- Object
- GraphQL::Execution::Interpreter::Runtime
- Defined in:
- lib/graphql/execution/interpreter/runtime.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
I think it would be even better if we could somehow make
continue_field
not recursive. “Trampolining” it somehow.
Defined Under Namespace
Modules: GraphQLResult Classes: GraphQLResultArray, GraphQLResultHash
Constant Summary collapse
- NO_ARGS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
{}.freeze
- HALT =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Object.new
Instance Attribute Summary collapse
-
#context ⇒ GraphQL::Query::Context
readonly
private
-
#progress_path ⇒ Object
readonly
private
-
#query ⇒ GraphQL::Query
readonly
private
-
#response ⇒ Hash
readonly
private
-
#schema ⇒ Class<GraphQL::Schema>
readonly
private
Instance Method Summary collapse
-
#after_lazy(lazy_obj, owner:, field:, path:, scoped_context:, owner_object:, arguments:, ast_node:, result:, result_name:, eager: false, trace: true, &block) ⇒ GraphQL::Execution::Lazy, Object
private
If loading
object
will be deferred, it’s a wrapper over it. -
#arguments(graphql_object, arg_owner, ast_node) ⇒ Object
private
-
#authorized_new(type, value, context) ⇒ Object
private
-
#continue_field(path, value, owner_type, field, current_type, ast_node, next_selections, is_non_null, owner_object, arguments, result_name, selection_result) ⇒ Lazy, ...
private
The resolver for
field
returnedvalue
. -
#continue_value(path, value, parent_type, field, is_non_null, ast_node, result_name, selection_result) ⇒ Object
private
rubocop:disable Metrics/ParameterLists.
-
#dead_result?(selection_result) ⇒ Boolean
private
-
#delete_interpreter_context(key) ⇒ Object
private
-
#directives_include?(node, graphql_object, parent_type) ⇒ Boolean
private
Check Schema::Directive.include? for each directive that’s present.
-
#evaluate_selection(path, result_name, field_ast_nodes_or_ast_node, scoped_context, owner_object, owner_type, is_eager_field, selections_result) ⇒ void
private
-
#evaluate_selection_with_args(kwarg_arguments, field_defn, next_path, ast_node, field_ast_nodes, scoped_context, owner_type, object, is_eager_field, result_name, selection_result) ⇒ Object
private
rubocop:disable Metrics/ParameterLists.
-
#evaluate_selections(path, scoped_context, owner_object, owner_type, is_eager_selection, gathered_selections, selections_result) ⇒ void
private
-
#gather_selections(owner_object, owner_type, selections, selections_by_name = {}) ⇒ Object
private
-
#initialize(query:) ⇒ Runtime
constructor
private
A new instance of Runtime.
-
#inspect ⇒ Object
private
-
#lazy?(object) ⇒ Boolean
private
-
#resolve_type(type, value, path) ⇒ Object
private
-
#resolve_with_directives(object, ast_node, &block) ⇒ Object
private
-
#run_directive(object, ast_node, idx, &block) ⇒ Object
private
-
#run_eager ⇒ void
private
This begins the execution.
-
#set_all_interpreter_context(object, field, arguments, path) ⇒ Object
private
-
#set_interpreter_context(key, value) ⇒ Object
private
Set this pair in the Query context, but also in the interpeter namespace, for compatibility.
-
#set_result(selection_result, result_name, value) ⇒ Object
private
Constructor Details
#initialize(query:) ⇒ Runtime
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.
Returns a new instance of Runtime.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/graphql/execution/interpreter/runtime.rb', line 45 def initialize(query:) @query = query @dataloader = query.multiplex.dataloader @schema = query.schema @context = query.context @multiplex_context = query.multiplex.context @interpreter_context = @context.namespace(:interpreter) @response = GraphQLResultHash.new # A cache of { Class => { String => Schema::Field } } # Which assumes that MyObject.get_field("myField") will return the same field # during the lifetime of a query @fields_cache = Hash.new { |h, k| h[k] = {} } # { Class => Boolean } @lazy_cache = {} end |
Instance Attribute Details
#context ⇒ GraphQL::Query::Context (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.
40 41 42 |
# File 'lib/graphql/execution/interpreter/runtime.rb', line 40 def context @context end |
#progress_path ⇒ 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.
176 177 178 |
# File 'lib/graphql/execution/interpreter/runtime.rb', line 176 def progress_path @progress_path end |
#query ⇒ GraphQL::Query (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.
34 35 36 |
# File 'lib/graphql/execution/interpreter/runtime.rb', line 34 def query @query end |
#response ⇒ Hash (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.
43 44 45 |
# File 'lib/graphql/execution/interpreter/runtime.rb', line 43 def response @response end |
#schema ⇒ Class<GraphQL::Schema> (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.
37 38 39 |
# File 'lib/graphql/execution/interpreter/runtime.rb', line 37 def schema @schema end |
Instance Method Details
#after_lazy(lazy_obj, owner:, field:, path:, scoped_context:, owner_object:, arguments:, ast_node:, result:, result_name:, eager: false, trace: true, &block) ⇒ GraphQL::Execution::Lazy, Object
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.
Returns If loading object
will be deferred, it’s a wrapper over it.
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 |
# File 'lib/graphql/execution/interpreter/runtime.rb', line 591 def after_lazy(lazy_obj, owner:, field:, path:, scoped_context:, owner_object:, arguments:, ast_node:, result:, result_name:, eager: false, trace: true, &block) if lazy?(lazy_obj) lazy = GraphQL::Execution::Lazy.new(path: path, field: field) do set_all_interpreter_context(owner_object, field, arguments, path) context.scoped_context = scoped_context # Wrap the execution of _this_ method with tracing, # but don't wrap the continuation below inner_obj = begin query.with_error_handling do if trace query.trace("execute_field_lazy", {owner: owner, field: field, path: path, query: query, object: owner_object, arguments: arguments, ast_node: ast_node}) do schema.sync_lazy(lazy_obj) end else schema.sync_lazy(lazy_obj) end end rescue GraphQL::ExecutionError, GraphQL::UnauthorizedError => err err end yield(inner_obj) end if eager lazy.value else set_result(result, result_name, lazy) lazy end else set_all_interpreter_context(owner_object, field, arguments, path) yield(lazy_obj) end end |
#arguments(graphql_object, arg_owner, ast_node) ⇒ Object
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.
626 627 628 629 630 631 632 633 |
# File 'lib/graphql/execution/interpreter/runtime.rb', line 626 def arguments(graphql_object, arg_owner, ast_node) if arg_owner.arguments_statically_coercible? query.arguments_for(ast_node, arg_owner) else # The arguments must be prepared in the context of the given object query.arguments_for(ast_node, arg_owner, parent_object: graphql_object) end end |
#authorized_new(type, value, context) ⇒ Object
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.
664 665 666 |
# File 'lib/graphql/execution/interpreter/runtime.rb', line 664 def (type, value, context) type.(value, context) end |
#continue_field(path, value, owner_type, field, current_type, ast_node, next_selections, is_non_null, owner_object, arguments, result_name, selection_result) ⇒ Lazy, ...
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.
The resolver for field
returned value
. Continue to execute the query,
treating value
as type
(probably the return type of the field).
Use next_selections
to resolve object fields, if there are any.
Location information from path
and ast_node
.
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 |
# File 'lib/graphql/execution/interpreter/runtime.rb', line 448 def continue_field(path, value, owner_type, field, current_type, ast_node, next_selections, is_non_null, owner_object, arguments, result_name, selection_result) # rubocop:disable Metrics/ParameterLists if current_type.non_null? current_type = current_type.of_type is_non_null = true end case current_type.kind.name when "SCALAR", "ENUM" r = current_type.coerce_result(value, context) set_result(selection_result, result_name, r) r when "UNION", "INTERFACE" resolved_type_or_lazy, resolved_value = resolve_type(current_type, value, path) resolved_value ||= value after_lazy(resolved_type_or_lazy, owner: current_type, path: path, ast_node: ast_node, scoped_context: context.scoped_context, field: field, owner_object: owner_object, arguments: arguments, trace: false, result_name: result_name, result: selection_result) do |resolved_type| possible_types = query.possible_types(current_type) if !possible_types.include?(resolved_type) parent_type = field.owner_type err_class = current_type::UnresolvedTypeError type_error = err_class.new(resolved_value, field, parent_type, resolved_type, possible_types) schema.type_error(type_error, context) set_result(selection_result, result_name, nil) nil else continue_field(path, resolved_value, owner_type, field, resolved_type, ast_node, next_selections, is_non_null, owner_object, arguments, result_name, selection_result) end end when "OBJECT" object_proxy = begin (current_type, value, context) rescue GraphQL::ExecutionError => err err end after_lazy(object_proxy, owner: current_type, path: path, ast_node: ast_node, scoped_context: context.scoped_context, field: field, owner_object: owner_object, arguments: arguments, trace: false, result_name: result_name, result: selection_result) do |inner_object| continue_value = continue_value(path, inner_object, owner_type, field, is_non_null, ast_node, result_name, selection_result) if HALT != continue_value response_hash = GraphQLResultHash.new response_hash.graphql_parent = selection_result response_hash.graphql_result_name = result_name set_result(selection_result, result_name, response_hash) gathered_selections = gather_selections(continue_value, current_type, next_selections) evaluate_selections(path, context.scoped_context, continue_value, current_type, false, gathered_selections, response_hash) response_hash end end when "LIST" inner_type = current_type.of_type response_list = GraphQLResultArray.new response_list.graphql_non_null_list_items = inner_type.non_null? response_list.graphql_parent = selection_result response_list.graphql_result_name = result_name set_result(selection_result, result_name, response_list) idx = 0 scoped_context = context.scoped_context begin value.each do |inner_value| next_path = path.dup next_path << idx this_idx = idx next_path.freeze idx += 1 # This will update `response_list` with the lazy after_lazy(inner_value, owner: inner_type, path: next_path, ast_node: ast_node, scoped_context: scoped_context, field: field, owner_object: owner_object, arguments: arguments, result_name: this_idx, result: response_list) do |inner_inner_value| continue_value = continue_value(next_path, inner_inner_value, owner_type, field, inner_type.non_null?, ast_node, this_idx, response_list) if HALT != continue_value continue_field(next_path, continue_value, owner_type, field, inner_type, ast_node, next_selections, false, owner_object, arguments, this_idx, response_list) end end end rescue NoMethodError => err # Ruby 2.2 doesn't have NoMethodError#receiver, can't check that one in this case. (It's been EOL since 2017.) if err.name == :each && (err.respond_to?(:receiver) ? err.receiver == value : true) # This happens when the GraphQL schema doesn't match the implementation. Help the dev debug. raise ListResultFailedError.new(value: value, field: field, path: path) else # This was some other NoMethodError -- let it bubble to reveal the real error. raise end end response_list else raise "Invariant: Unhandled type kind #{current_type.kind} (#{current_type})" end end |
#continue_value(path, value, parent_type, field, is_non_null, ast_node, result_name, selection_result) ⇒ Object
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.
rubocop:disable Metrics/ParameterLists
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 |
# File 'lib/graphql/execution/interpreter/runtime.rb', line 375 def continue_value(path, value, parent_type, field, is_non_null, ast_node, result_name, selection_result) # rubocop:disable Metrics/ParameterLists case value when nil if is_non_null set_result(selection_result, result_name, nil) do # This block is called if `result_name` is not dead. (Maybe a previous invalid nil caused it be marked dead.) err = parent_type::InvalidNullError.new(parent_type, field, value) schema.type_error(err, context) end else set_result(selection_result, result_name, nil) end HALT when GraphQL::Error # Handle these cases inside a single `when` # to avoid the overhead of checking three different classes # every time. if value.is_a?(GraphQL::ExecutionError) if !dead_result?(selection_result) value.path ||= path value.ast_node ||= ast_node context.errors << value set_result(selection_result, result_name, nil) end HALT elsif value.is_a?(GraphQL::UnauthorizedError) # this hook might raise & crash, or it might return # a replacement value next_value = begin schema.(value) rescue GraphQL::ExecutionError => err err end continue_value(path, next_value, parent_type, field, is_non_null, ast_node, result_name, selection_result) elsif GraphQL::Execution::Execute::SKIP == value HALT else # What could this actually _be_? Anyhow, # preserve the default behavior of doing nothing with it. value end when Array # It's an array full of execution errors; add them all. if value.any? && value.all? { |v| v.is_a?(GraphQL::ExecutionError) } if !dead_result?(selection_result) value.each_with_index do |error, index| error.ast_node ||= ast_node error.path ||= path + (field.type.list? ? [index] : []) context.errors << error end set_result(selection_result, result_name, nil) end HALT else value end when GraphQL::Execution::Interpreter::RawValue # Write raw value directly to the response without resolving nested objects set_result(selection_result, result_name, value.resolve) HALT else value end end |
#dead_result?(selection_result) ⇒ Boolean
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.
331 332 333 334 335 336 337 338 339 340 341 |
# File 'lib/graphql/execution/interpreter/runtime.rb', line 331 def dead_result?(selection_result) r = selection_result while r if r.graphql_dead return true else r = r.graphql_parent end end false end |
#delete_interpreter_context(key) ⇒ Object
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.
642 643 644 645 |
# File 'lib/graphql/execution/interpreter/runtime.rb', line 642 def delete_interpreter_context(key) @interpreter_context.delete(key) @context.delete(key) end |
#directives_include?(node, graphql_object, parent_type) ⇒ Boolean
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.
Check Schema::Directive.include? for each directive that’s present
559 560 561 562 563 564 565 566 567 568 |
# File 'lib/graphql/execution/interpreter/runtime.rb', line 559 def directives_include?(node, graphql_object, parent_type) node.directives.each do |dir_node| dir_defn = schema.directives.fetch(dir_node.name).type_class || raise("Only class-based directives are supported (not #{dir_node.name.inspect})") args = arguments(graphql_object, dir_defn, dir_node).keyword_arguments if !dir_defn.include?(graphql_object, args, context) return false end end true end |
#evaluate_selection(path, result_name, field_ast_nodes_or_ast_node, scoped_context, owner_object, owner_type, is_eager_field, selections_result) ⇒ void
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.
This method returns an undefined value.
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/graphql/execution/interpreter/runtime.rb', line 179 def evaluate_selection(path, result_name, field_ast_nodes_or_ast_node, scoped_context, owner_object, owner_type, is_eager_field, selections_result) # rubocop:disable Metrics/ParameterLists # As a performance optimization, the hash key will be a `Node` if # there's only one selection of the field. But if there are multiple # selections of the field, it will be an Array of nodes if field_ast_nodes_or_ast_node.is_a?(Array) field_ast_nodes = field_ast_nodes_or_ast_node ast_node = field_ast_nodes.first else field_ast_nodes = nil ast_node = field_ast_nodes_or_ast_node end field_name = ast_node.name field_defn = @fields_cache[owner_type][field_name] ||= owner_type.get_field(field_name) is_introspection = false if field_defn.nil? field_defn = if owner_type == schema.query && (entry_point_field = schema.introspection_system.entry_point(name: field_name)) is_introspection = true entry_point_field elsif (dynamic_field = schema.introspection_system.dynamic_field(name: field_name)) is_introspection = true dynamic_field else raise "Invariant: no field for #{owner_type}.#{field_name}" end end return_type = field_defn.type next_path = path.dup next_path << result_name next_path.freeze # This seems janky, but we need to know # the field's return type at this path in order # to propagate `null` if return_type.non_null? (selections_result.graphql_non_null_field_names ||= []).push(result_name) end # Set this before calling `run_with_directives`, so that the directive can have the latest path set_all_interpreter_context(nil, field_defn, nil, next_path) context.scoped_context = scoped_context object = owner_object if is_introspection object = (field_defn.owner, object, context) end total_args_count = field_defn.arguments.size if total_args_count == 0 kwarg_arguments = GraphQL::Execution::Interpreter::Arguments::EMPTY evaluate_selection_with_args(kwarg_arguments, field_defn, next_path, ast_node, field_ast_nodes, scoped_context, owner_type, object, is_eager_field, result_name, selections_result) else # TODO remove all arguments(...) usages? @query.arguments_cache.dataload_for(ast_node, field_defn, object) do |resolved_arguments| evaluate_selection_with_args(resolved_arguments, field_defn, next_path, ast_node, field_ast_nodes, scoped_context, owner_type, object, is_eager_field, result_name, selections_result) end end end |
#evaluate_selection_with_args(kwarg_arguments, field_defn, next_path, ast_node, field_ast_nodes, scoped_context, owner_type, object, is_eager_field, result_name, selection_result) ⇒ Object
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.
rubocop:disable Metrics/ParameterLists
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/graphql/execution/interpreter/runtime.rb', line 238 def evaluate_selection_with_args(kwarg_arguments, field_defn, next_path, ast_node, field_ast_nodes, scoped_context, owner_type, object, is_eager_field, result_name, selection_result) # rubocop:disable Metrics/ParameterLists context.scoped_context = scoped_context return_type = field_defn.type after_lazy(kwarg_arguments, owner: owner_type, field: field_defn, path: next_path, ast_node: ast_node, scoped_context: context.scoped_context, owner_object: object, arguments: kwarg_arguments, result_name: result_name, result: selection_result) do |resolved_arguments| if resolved_arguments.is_a?(GraphQL::ExecutionError) || resolved_arguments.is_a?(GraphQL::UnauthorizedError) continue_value(next_path, resolved_arguments, owner_type, field_defn, return_type.non_null?, ast_node, result_name, selection_result) next end kwarg_arguments = if resolved_arguments.empty? && field_defn.extras.empty? # We can avoid allocating the `{ Symbol => Object }` hash in this case NO_ARGS else # Bundle up the extras, then make a new arguments instance # that includes the extras, too. extra_args = {} field_defn.extras.each do |extra| case extra when :ast_node extra_args[:ast_node] = ast_node when :execution_errors extra_args[:execution_errors] = ExecutionErrors.new(context, ast_node, next_path) when :path extra_args[:path] = next_path when :lookahead if !field_ast_nodes field_ast_nodes = [ast_node] end extra_args[:lookahead] = Execution::Lookahead.new( query: query, ast_nodes: field_ast_nodes, field: field_defn, ) when :argument_details # Use this flag to tell Interpreter::Arguments to add itself # to the keyword args hash _before_ freezing everything. extra_args[:argument_details] = :__arguments_add_self when :irep_node # This is used by `__typename` in order to support the legacy runtime, # but it has no use here (and it's always `nil`). # Stop adding it here to avoid the overhead of `.merge_extras` below. else extra_args[extra] = field_defn.fetch_extra(extra, context) end end if extra_args.any? resolved_arguments = resolved_arguments.merge_extras(extra_args) end resolved_arguments.keyword_arguments end set_all_interpreter_context(nil, nil, kwarg_arguments, nil) # Optimize for the case that field is selected only once if field_ast_nodes.nil? || field_ast_nodes.size == 1 next_selections = ast_node.selections else next_selections = [] field_ast_nodes.each { |f| next_selections.concat(f.selections) } end field_result = resolve_with_directives(object, ast_node) do # Actually call the field resolver and capture the result app_result = begin query.with_error_handling do query.trace("execute_field", {owner: owner_type, field: field_defn, path: next_path, ast_node: ast_node, query: query, object: object, arguments: kwarg_arguments}) do field_defn.resolve(object, kwarg_arguments, context) end end rescue GraphQL::ExecutionError => err err end after_lazy(app_result, owner: owner_type, field: field_defn, path: next_path, ast_node: ast_node, scoped_context: context.scoped_context, owner_object: object, arguments: kwarg_arguments, result_name: result_name, result: selection_result) do |inner_result| continue_value = continue_value(next_path, inner_result, owner_type, field_defn, return_type.non_null?, ast_node, result_name, selection_result) if HALT != continue_value continue_field(next_path, continue_value, owner_type, field_defn, return_type, ast_node, next_selections, false, object, kwarg_arguments, result_name, selection_result) end end end # If this field is a root mutation field, immediately resolve # all of its child fields before moving on to the next root mutation field. # (Subselections of this mutation will still be resolved level-by-level.) if is_eager_field Interpreter::Resolve.resolve_all([field_result], @dataloader) else # Return this from `after_lazy` because it might be another lazy that needs to be resolved field_result end end end |
#evaluate_selections(path, scoped_context, owner_object, owner_type, is_eager_selection, gathered_selections, selections_result) ⇒ void
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.
This method returns an undefined value.
162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/graphql/execution/interpreter/runtime.rb', line 162 def evaluate_selections(path, scoped_context, owner_object, owner_type, is_eager_selection, gathered_selections, selections_result) set_all_interpreter_context(owner_object, nil, nil, path) gathered_selections.each do |result_name, field_ast_nodes_or_ast_node| @dataloader.append_job { evaluate_selection( path, result_name, field_ast_nodes_or_ast_node, scoped_context, owner_object, owner_type, is_eager_selection, selections_result ) } end nil end |
#gather_selections(owner_object, owner_type, selections, selections_by_name = {}) ⇒ Object
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.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/graphql/execution/interpreter/runtime.rb', line 104 def gather_selections(owner_object, owner_type, selections, selections_by_name = {}) selections.each do |node| # Skip gathering this if the directive says so if !directives_include?(node, owner_object, owner_type) next end case node when GraphQL::Language::Nodes::Field response_key = node.alias || node.name selections = selections_by_name[response_key] # if there was already a selection of this field, # use an array to hold all selections, # otherise, use the single node to represent the selection if selections # This field was already selected at least once, # add this node to the list of selections s = Array(selections) s << node selections_by_name[response_key] = s else # No selection was found for this field yet selections_by_name[response_key] = node end when GraphQL::Language::Nodes::InlineFragment if node.type type_defn = schema.get_type(node.type.name) # Faster than .map{}.include?() query.warden.possible_types(type_defn).each do |t| if t == owner_type gather_selections(owner_object, owner_type, node.selections, selections_by_name) break end end else # it's an untyped fragment, definitely continue gather_selections(owner_object, owner_type, node.selections, selections_by_name) end when GraphQL::Language::Nodes::FragmentSpread fragment_def = query.fragments[node.name] type_defn = schema.get_type(fragment_def.type.name) possible_types = query.warden.possible_types(type_defn) possible_types.each do |t| if t == owner_type gather_selections(owner_object, owner_type, fragment_def.selections, selections_by_name) break end end else raise "Invariant: unexpected selection class: #{node.class}" end end selections_by_name end |
#inspect ⇒ Object
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.
61 62 63 |
# File 'lib/graphql/execution/interpreter/runtime.rb', line 61 def inspect "#<#{self.class.name} response=#{@response.inspect}>" end |
#lazy?(object) ⇒ Boolean
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.
668 669 670 671 672 |
# File 'lib/graphql/execution/interpreter/runtime.rb', line 668 def lazy?(object) @lazy_cache.fetch(object.class) { @lazy_cache[object.class] = @schema.lazy?(object) } end |
#resolve_type(type, value, path) ⇒ Object
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.
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 |
# File 'lib/graphql/execution/interpreter/runtime.rb', line 647 def resolve_type(type, value, path) trace_payload = { context: context, type: type, object: value, path: path } resolved_type, resolved_value = query.trace("resolve_type", trace_payload) do query.resolve_type(type, value) end if lazy?(resolved_type) GraphQL::Execution::Lazy.new do query.trace("resolve_type_lazy", trace_payload) do schema.sync_lazy(resolved_type) end end else [resolved_type, resolved_value] end end |
#resolve_with_directives(object, ast_node, &block) ⇒ Object
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.
537 538 539 540 |
# File 'lib/graphql/execution/interpreter/runtime.rb', line 537 def resolve_with_directives(object, ast_node, &block) return yield if ast_node.directives.empty? run_directive(object, ast_node, 0, &block) end |
#run_directive(object, ast_node, idx, &block) ⇒ Object
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.
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 |
# File 'lib/graphql/execution/interpreter/runtime.rb', line 542 def run_directive(object, ast_node, idx, &block) dir_node = ast_node.directives[idx] if !dir_node yield else dir_defn = schema.directives.fetch(dir_node.name) if !dir_defn.is_a?(Class) dir_defn = dir_defn.type_class || raise("Only class-based directives are supported (not `@#{dir_node.name}`)") end dir_args = arguments(nil, dir_defn, dir_node).keyword_arguments dir_defn.resolve(object, dir_args, context) do run_directive(object, ast_node, idx + 1, &block) end end end |
#run_eager ⇒ void
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.
This method returns an undefined value.
This begins the execution. Some deferred work might be stored up in lazies.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/graphql/execution/interpreter/runtime.rb', line 68 def run_eager root_operation = query.selected_operation root_op_type = root_operation.operation_type || "query" root_type = schema.root_type_for_operation(root_op_type) path = [] set_all_interpreter_context(query.root_value, nil, nil, path) object_proxy = (root_type, query.root_value, context) object_proxy = schema.sync_lazy(object_proxy) if object_proxy.nil? # Root .authorized? returned false. @response = nil else resolve_with_directives(object_proxy, root_operation) do # execute query level directives gathered_selections = gather_selections(object_proxy, root_type, root_operation.selections) # Make the first fiber which will begin execution @dataloader.append_job { evaluate_selections( path, context.scoped_context, object_proxy, root_type, root_op_type == "mutation", gathered_selections, @response, ) } end end delete_interpreter_context(:current_path) delete_interpreter_context(:current_field) delete_interpreter_context(:current_object) delete_interpreter_context(:current_arguments) nil end |
#set_all_interpreter_context(object, field, arguments, path) ⇒ Object
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.
570 571 572 573 574 575 576 577 578 579 580 581 582 583 |
# File 'lib/graphql/execution/interpreter/runtime.rb', line 570 def set_all_interpreter_context(object, field, arguments, path) if object @context[:current_object] = @interpreter_context[:current_object] = object end if field @context[:current_field] = @interpreter_context[:current_field] = field end if arguments @context[:current_arguments] = @interpreter_context[:current_arguments] = arguments end if path @context[:current_path] = @interpreter_context[:current_path] = path end end |
#set_interpreter_context(key, value) ⇒ Object
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.
Set this pair in the Query context, but also in the interpeter namespace, for compatibility.
637 638 639 640 |
# File 'lib/graphql/execution/interpreter/runtime.rb', line 637 def set_interpreter_context(key, value) @interpreter_context[key] = value @context[key] = value end |
#set_result(selection_result, result_name, value) ⇒ Object
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.
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/graphql/execution/interpreter/runtime.rb', line 343 def set_result(selection_result, result_name, value) if !dead_result?(selection_result) if value.nil? && ( # there are two conditions under which `nil` is not allowed in the response: (selection_result.graphql_non_null_list_items) || # this value would be written into a list that doesn't allow nils ((nn = selection_result.graphql_non_null_field_names) && nn.include?(result_name)) # this value would be written into a field that doesn't allow nils ) # This is an invalid nil that should be propagated # One caller of this method passes a block, # namely when application code returns a `nil` to GraphQL and it doesn't belong there. # The other possibility for reaching here is when a field returns an ExecutionError, so we write # `nil` to the response, not knowing whether it's an invalid `nil` or not. # (And in that case, we don't have to call the schema's handler, since it's not a bug in the application.) # TODO the code is trying to tell me something. yield if block_given? parent = selection_result.graphql_parent name_in_parent = selection_result.graphql_result_name if parent.nil? # This is a top-level result hash @response = nil else set_result(parent, name_in_parent, nil) # This is odd, but it's how it used to work. Even if `parent` _would_ accept # a `nil`, it's marked dead. TODO: check the spec, is there a reason for this? parent.graphql_dead = true end else selection_result[result_name] = value end end end |