Class: GraphQL::Execution::Next::FieldResolveStep
- Inherits:
-
Object
- Object
- GraphQL::Execution::Next::FieldResolveStep
- Defined in:
- lib/graphql/execution/next/field_resolve_step.rb
Direct Known Subclasses
Defined Under Namespace
Modules: AlwaysAuthorized
Instance Attribute Summary collapse
-
#arguments ⇒ Object
Returns the value of attribute arguments.
-
#ast_node ⇒ Object
readonly
Returns the value of attribute ast_node.
-
#field_definition ⇒ Object
readonly
Returns the value of attribute field_definition.
-
#field_results ⇒ Object
readonly
Returns the value of attribute field_results.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#object_is_authorized ⇒ Object
readonly
Returns the value of attribute object_is_authorized.
-
#parent_type ⇒ Object
readonly
Returns the value of attribute parent_type.
-
#pending_steps ⇒ Object
Returns the value of attribute pending_steps.
-
#runner ⇒ Object
readonly
Returns the value of attribute runner.
-
#selections_step ⇒ Object
readonly
Returns the value of attribute selections_step.
-
#was_scoped ⇒ Object
readonly
Returns the value of attribute was_scoped.
Instance Method Summary collapse
-
#add_graphql_error(err) ⇒ Object
-
#add_non_null_error(is_from_array) ⇒ Object
-
#any_lazy_results? ⇒ Boolean
-
#append_selection(ast_node) ⇒ Object
-
#ast_nodes ⇒ Object
-
#authorized_finished(step) ⇒ Object
-
#build_arguments ⇒ Object
-
#build_results ⇒ Object
-
#call ⇒ Object
-
#coerce_argument_value(arguments, arg_defn, arg_value, run_loads, target_keyword: run_loads ? arg_defn.keyword : arg_defn.graphql_name, as_type: nil) ⇒ Object
-
#coerce_arguments(argument_owner, ast_arguments_or_hash, run_loads = true) ⇒ Object
-
#enqueue_next_steps ⇒ Object
-
#execute_field ⇒ Object
-
#finish_extensions ⇒ Object
-
#initialize(parent_type:, runner:, key:, selections_step:) ⇒ FieldResolveStep
constructor
A new instance of FieldResolveStep.
-
#path ⇒ Object
-
#sync(lazy) ⇒ Object
-
#value ⇒ Object
Implement that Lazy API.
Constructor Details
#initialize(parent_type:, runner:, key:, selections_step:) ⇒ FieldResolveStep
Returns a new instance of FieldResolveStep.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/graphql/execution/next/field_resolve_step.rb', line 6 def initialize(parent_type:, runner:, key:, selections_step:) @selections_step = selections_step @key = key @parent_type = parent_type @ast_node = @ast_nodes = nil @runner = runner @field_definition = nil @arguments = nil @field_results = nil @path = nil @enqueued_authorization = false @all_next_objects = nil @all_next_results = nil @static_type = nil @next_selections = nil @object_is_authorized = nil @finish_extension_idx = nil @was_scoped = nil @pending_steps = nil end |
Instance Attribute Details
#arguments ⇒ Object
Returns the value of attribute arguments.
30 31 32 |
# File 'lib/graphql/execution/next/field_resolve_step.rb', line 30 def arguments @arguments end |
#ast_node ⇒ Object (readonly)
Returns the value of attribute ast_node.
27 28 29 |
# File 'lib/graphql/execution/next/field_resolve_step.rb', line 27 def ast_node @ast_node end |
#field_definition ⇒ Object (readonly)
Returns the value of attribute field_definition.
27 28 29 |
# File 'lib/graphql/execution/next/field_resolve_step.rb', line 27 def field_definition @field_definition end |
#field_results ⇒ Object (readonly)
Returns the value of attribute field_results.
27 28 29 |
# File 'lib/graphql/execution/next/field_resolve_step.rb', line 27 def field_results @field_results end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
27 28 29 |
# File 'lib/graphql/execution/next/field_resolve_step.rb', line 27 def key @key end |
#object_is_authorized ⇒ Object (readonly)
Returns the value of attribute object_is_authorized.
27 28 29 |
# File 'lib/graphql/execution/next/field_resolve_step.rb', line 27 def @object_is_authorized end |
#parent_type ⇒ Object (readonly)
Returns the value of attribute parent_type.
27 28 29 |
# File 'lib/graphql/execution/next/field_resolve_step.rb', line 27 def parent_type @parent_type end |
#pending_steps ⇒ Object
Returns the value of attribute pending_steps.
30 31 32 |
# File 'lib/graphql/execution/next/field_resolve_step.rb', line 30 def pending_steps @pending_steps end |
#runner ⇒ Object (readonly)
Returns the value of attribute runner.
27 28 29 |
# File 'lib/graphql/execution/next/field_resolve_step.rb', line 27 def runner @runner end |
#selections_step ⇒ Object (readonly)
Returns the value of attribute selections_step.
27 28 29 |
# File 'lib/graphql/execution/next/field_resolve_step.rb', line 27 def selections_step @selections_step end |
#was_scoped ⇒ Object (readonly)
Returns the value of attribute was_scoped.
27 28 29 |
# File 'lib/graphql/execution/next/field_resolve_step.rb', line 27 def was_scoped @was_scoped end |
Instance Method Details
#add_graphql_error(err) ⇒ Object
266 267 268 269 270 271 |
# File 'lib/graphql/execution/next/field_resolve_step.rb', line 266 def add_graphql_error(err) err.path = path err.ast_nodes = ast_nodes @selections_step.query.context.add_error(err) err end |
#add_non_null_error(is_from_array) ⇒ Object
585 586 587 588 |
# File 'lib/graphql/execution/next/field_resolve_step.rb', line 585 def add_non_null_error(is_from_array) err = InvalidNullError.new(@parent_type, @field_definition, ast_nodes, is_from_array: is_from_array, path: path) @runner.schema.type_error(err, @selections_step.query.context) end |
#any_lazy_results? ⇒ Boolean
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 |
# File 'lib/graphql/execution/next/field_resolve_step.rb', line 397 def any_lazy_results? lazies = false if @runner.resolves_lazies # TODO extract this @field_results.each do |field_result| if @runner.lazy?(field_result) lazies = true break elsif field_result.is_a?(Array) field_result.each do |inner_fr| if @runner.lazy?(inner_fr) break lazies = true end end if lazies break end end end end lazies end |
#append_selection(ast_node) ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/graphql/execution/next/field_resolve_step.rb', line 40 def append_selection(ast_node) if @ast_node.nil? @ast_node = ast_node elsif @ast_nodes.nil? @ast_nodes = [@ast_node, ast_node] else @ast_nodes << ast_node end nil end |
#ast_nodes ⇒ Object
36 37 38 |
# File 'lib/graphql/execution/next/field_resolve_step.rb', line 36 def ast_nodes @ast_nodes ||= [@ast_node] end |
#authorized_finished(step) ⇒ Object
578 579 580 581 582 583 |
# File 'lib/graphql/execution/next/field_resolve_step.rb', line 578 def (step) @pending_steps.delete(step) if @enqueued_authorization && @pending_steps.size == 0 @runner.add_step(self) end end |
#build_arguments ⇒ Object
279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/graphql/execution/next/field_resolve_step.rb', line 279 def build_arguments query = @selections_step.query field_name = @ast_node.name @field_definition = query.get_field(@parent_type, field_name) || raise("Invariant: no field found for #{@parent_type.to_type_signature}.#{ast_node.name}") arguments = coerce_arguments(@field_definition, @ast_node.arguments) # rubocop:disable Development/ContextIsPassedCop @arguments ||= arguments # may have already been set to an error if (@pending_steps.nil? || @pending_steps.size == 0) && @field_results.nil? # Make sure the arguments flow didn't already call through execute_field end end |
#build_results ⇒ Object
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 |
# File 'lib/graphql/execution/next/field_resolve_step.rb', line 460 def build_results return_type = @field_definition.type return_result_type = return_type.unwrap if return_result_type.kind.composite? @static_type = return_result_type if @ast_nodes @next_selections = [] @ast_nodes.each do |ast_node| @next_selections.concat(ast_node.selections) end else @next_selections = @ast_node.selections end @all_next_objects = [] @all_next_results = [] is_list = return_type.list? is_non_null = return_type.non_null? results = @selections_step.results field_result_idx = 0 i = 0 s = results.size while i < s do result_h = results[i] if @object_is_authorized[i] result = @field_results[field_result_idx] field_result_idx += 1 else result = nil end i += 1 build_graphql_result(result_h, @key, result, return_type, is_non_null, is_list, false) end @enqueued_authorization = true if @pending_steps.nil? || @pending_steps.size == 0 enqueue_next_steps else # Do nothing -- it will enqueue itself later end else ctx = @selections_step.query.context results = @selections_step.results field_result_idx = 0 i = 0 s = results.size while i < s do result_h = results[i] if @object_is_authorized[i] field_result = @field_results[field_result_idx] field_result_idx += 1 else field_result = nil end i += 1 result_h[@key] = if field_result.nil? if return_type.non_null? add_non_null_error(false) else nil end elsif field_result.is_a?(GraphQL::Error) add_graphql_error(field_result) else # TODO `nil`s in [T!] types aren't handled return_type.coerce_result(field_result, ctx) end end end end |
#call ⇒ Object
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/graphql/execution/next/field_resolve_step.rb', line 245 def call if @enqueued_authorization enqueue_next_steps elsif @finish_extension_idx finish_extensions elsif @field_results build_results elsif @arguments execute_field else build_arguments end rescue StandardError => err if @field_definition && !err..start_with?("Resolving ") # TODO remove this check ^^^^^^ when NullDataloader isn't recursive raise err, "Resolving #{@field_definition.path}: #{err.}", err.backtrace else raise end end |
#coerce_argument_value(arguments, arg_defn, arg_value, run_loads, target_keyword: run_loads ? arg_defn.keyword : arg_defn.graphql_name, as_type: nil) ⇒ Object
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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 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 |
# File 'lib/graphql/execution/next/field_resolve_step.rb', line 110 def coerce_argument_value(arguments, arg_defn, arg_value, run_loads, target_keyword: run_loads ? arg_defn.keyword : arg_defn.graphql_name, as_type: nil) arg_t = as_type || arg_defn.type if arg_t.non_null? arg_t = arg_t.of_type end if arg_value.is_a?(Language::Nodes::VariableIdentifier) vars = @selections_step.query.variables arg_value = if vars.key?(arg_value.name) vars[arg_value.name] elsif vars.key?(arg_value.name.to_sym) vars[arg_value.name.to_sym] else nil end end if arg_value.is_a?(Language::Nodes::NullValue) arg_value = nil elsif arg_value.is_a?(Language::Nodes::Enum) arg_value = arg_value.name end ctx = @selections_step.query.context arg_value = if arg_t.list? if arg_value.nil? arg_value else arg_value = Array(arg_value) inner_t = arg_t.of_type result = Array.new(arg_value.size) arg_value.each_with_index { |v, i| coerce_argument_value(result, arg_defn, v, run_loads, target_keyword: i, as_type: inner_t) } result end elsif arg_t.kind.leaf? begin arg_t.coerce_input(arg_value, ctx) rescue GraphQL::UnauthorizedEnumValueError => enum_err begin @runner.schema.(enum_err) rescue GraphQL::ExecutionError => ex_err ex_err end end elsif arg_t.kind.input_object? input_obj_vals = arg_value.is_a?(Language::Nodes::InputObject) ? arg_value.arguments : arg_value # rubocop:disable Development/ContextIsPassedCop input_obj_args = coerce_arguments(arg_t, input_obj_vals) arg_t.new(nil, ruby_kwargs: input_obj_args, context: @selections_step.query.context, defaults_used: nil) else raise "Unsupported argument value: #{arg_t.to_type_signature} / #{arg_value.class} (#{arg_value.inspect})" end if as_type.nil? # only on root arguments, not list elements arg_value = begin begin arg_defn.prepare_value(nil, arg_value, context: ctx) rescue StandardError => err @runner.schema.handle_or_reraise(ctx, err) end rescue GraphQL::ExecutionError => exec_err exec_err end end if arg_value.is_a?(GraphQL::Error) @arguments = arg_value elsif run_loads && arg_defn.loads && as_type.nil? && !arg_value.nil? # This is for legacy compat: load_receiver = if (r = @field_definition.resolver) r.new(field: @field_definition, context: @selections_step.query.context, object: nil) else @field_definition end @pending_steps ||= [] if arg_t.list? results = Array.new(arg_value.size, nil) arguments[arg_defn.keyword] = results arg_value.each_with_index do |inner_v, idx| loads_step = LoadArgumentStep.new( field_resolve_step: self, load_receiver: load_receiver, argument_value: inner_v, argument_definition: arg_defn, arguments: results, argument_key: idx, ) @pending_steps.push(loads_step) @runner.add_step(loads_step) end else loads_step = LoadArgumentStep.new( field_resolve_step: self, load_receiver: load_receiver, argument_value: arg_value, argument_definition: arg_defn, arguments: arguments, argument_key: arg_defn.keyword, ) @pending_steps.push(loads_step) @runner.add_step(loads_step) end else arguments[target_keyword] = arg_value end nil end |
#coerce_arguments(argument_owner, ast_arguments_or_hash, run_loads = true) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 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 103 104 105 106 107 108 |
# File 'lib/graphql/execution/next/field_resolve_step.rb', line 51 def coerce_arguments(argument_owner, ast_arguments_or_hash, run_loads = true) arg_defns = @selections_step.query.types.arguments(argument_owner) if arg_defns.empty? return EmptyObjects::EMPTY_HASH end args_hash = {} if ast_arguments_or_hash.nil? # This can happen with `.trigger` return args_hash end arg_inputs_are_h = ast_arguments_or_hash.is_a?(Hash) arg_defns.each do |arg_defn| arg_value = nil was_found = false if arg_inputs_are_h ast_arguments_or_hash.each do |key, value| if key == arg_defn.keyword || key.to_s == arg_defn.graphql_name arg_value = value was_found = true break end end else ast_arguments_or_hash.each do |arg_node| if arg_node.name == arg_defn.graphql_name arg_value = arg_node.value was_found = true break end end end if arg_value.is_a?(Language::Nodes::VariableIdentifier) vars = @selections_step.query.variables arg_value = if vars.key?(arg_value.name) vars[arg_value.name] elsif vars.key?(arg_value.name.to_sym) vars[arg_value.name.to_sym] else was_found = false nil end end if !was_found && arg_defn.default_value? was_found = true arg_value = arg_defn.default_value end if was_found coerce_argument_value(args_hash, arg_defn, arg_value, run_loads) end end args_hash end |
#enqueue_next_steps ⇒ Object
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 |
# File 'lib/graphql/execution/next/field_resolve_step.rb', line 533 def enqueue_next_steps if !@all_next_results.empty? @all_next_objects.compact! if @static_type.kind.abstract? next_objects_by_type = Hash.new { |h, obj_t| h[obj_t] = [] }.compare_by_identity next_results_by_type = Hash.new { |h, obj_t| h[obj_t] = [] }.compare_by_identity @all_next_objects.each_with_index do |next_object, i| result = @all_next_results[i] if (object_type = @runner.runtime_type_at[result]) # OK else object_type = @runner.resolve_type(@static_type, next_object, @selections_step.query) @runner.runtime_type_at[result] = object_type end next_objects_by_type[object_type] << next_object next_results_by_type[object_type] << result end next_objects_by_type.each do |obj_type, next_objects| @runner.add_step(SelectionsStep.new( path: path, parent_type: obj_type, selections: @next_selections, objects: next_objects, results: next_results_by_type[obj_type], runner: @runner, query: @selections_step.query, )) end else @runner.add_step(SelectionsStep.new( path: path, parent_type: @static_type, selections: @next_selections, objects: @all_next_objects, results: @all_next_results, runner: @runner, query: @selections_step.query, )) end end end |
#execute_field ⇒ Object
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 330 331 332 333 334 335 336 337 338 339 340 341 342 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 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/graphql/execution/next/field_resolve_step.rb', line 292 def execute_field objects = @selections_step.objects # TODO not as good because only one error? if @arguments.is_a?(GraphQL::Error) @field_results = Array.new(objects.size, @arguments) @object_is_authorized = AlwaysAuthorized build_results return end query = @selections_step.query ctx = query.context if (v = @field_definition.validators).any? # rubocop:disable Development/NoneWithoutBlockCop begin Schema::Validator.validate!(v, nil, ctx, @arguments) rescue GraphQL::RuntimeError => err @field_results = Array.new(objects.size, err) @object_is_authorized = AlwaysAuthorized build_results return end end @field_definition.extras.each do |extra| case extra when :lookahead if @arguments.frozen? @arguments = @arguments.dup end @arguments[:lookahead] = Execution::Lookahead.new( query: query, ast_nodes: ast_nodes, field: @field_definition, ) when :ast_node if @arguments.frozen? @arguments = @arguments.dup end @arguments[:ast_node] = ast_node else raise ArgumentError, "This `extra` isn't supported yet: #{extra.inspect}. Open an issue on GraphQL-Ruby to add compatibility for it." end end if @field_definition.dynamic_introspection # TODO break this backwards compat somehow? objects = @selections_step.graphql_objects end if @runner. && @runner.(@field_definition, ctx) = [] @object_is_authorized = objects.map { |o| is_authed = @field_definition.(o, @arguments, ctx) if is_authed << o else begin err = GraphQL::UnauthorizedFieldError.new(object: o, type: @parent_type, context: ctx, field: @field_definition) << query.schema.(err) is_authed = true rescue GraphQL::ExecutionError => exec_err add_graphql_error(exec_err) end end is_authed } if .size == 0 return end else = objects @object_is_authorized = AlwaysAuthorized end if @parent_type.default_relay? && .all? { |o| o.respond_to?(:was_authorized_by_scope_items?) && o. } @was_scoped = true end query.current_trace.begin_execute_field(@field_definition, @arguments, , query) has_extensions = @field_definition.extensions.size > 0 if has_extensions @extended = GraphQL::Schema::Field::ExtendedState.new(@arguments, ) @field_results = @field_definition.run_next_extensions_before_resolve(, @arguments, ctx, @extended) do |objs, args| if (added_extras = @extended.added_extras) args = args.dup added_extras.each { |e| args.delete(e) } end resolve_batch(objs, ctx, args) end @finish_extension_idx = 0 else @field_results = resolve_batch(, ctx, @arguments) end query.current_trace.end_execute_field(@field_definition, @arguments, , query, @field_results) if any_lazy_results? @runner.dataloader.lazy_at_depth(path.size, self) elsif has_extensions finish_extensions elsif @pending_steps.nil? || @pending_steps.empty? build_results end end |
#finish_extensions ⇒ Object
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 |
# File 'lib/graphql/execution/next/field_resolve_step.rb', line 419 def finish_extensions ctx = @selections_step.query.context memos = @extended.memos || EmptyObjects::EMPTY_HASH while ext = @field_definition.extensions[@finish_extension_idx] # These two are hardcoded here because of how they need to interact with runtime metadata. # It would probably be better case ext when Schema::Field::ConnectionExtension conns = ctx.schema.connections @field_results = @field_results.map.each_with_index do |value, idx| object = @extended.object[idx] conn = conns.populate_connection(@field_definition, object, value, @arguments, ctx) if conn conn. = @was_scoped end conn end when Schema::Field::ScopeExtension if @was_scoped.nil? if (rt = @field_definition.type.unwrap).respond_to?(:scope_items) @was_scoped = true @field_results = @field_results.map { |v| v.nil? ? v : rt.scope_items(v, ctx) } else @was_scoped = false end end else memo = memos[@finish_extension_idx] @field_results = ext.after_resolve_next(objects: @extended.object, arguments: @extended.arguments, context: ctx, values: @field_results, memo: memo) # rubocop:disable Development/ContextIsPassedCop end @finish_extension_idx += 1 if any_lazy_results? @runner.dataloader.lazy_at_depth(path.size, self) return end end @finish_extension_idx = nil build_results end |
#path ⇒ Object
32 33 34 |
# File 'lib/graphql/execution/next/field_resolve_step.rb', line 32 def path @path ||= [*@selections_step.path, @key].freeze end |
#sync(lazy) ⇒ Object
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/graphql/execution/next/field_resolve_step.rb', line 227 def sync(lazy) if lazy.is_a?(Array) lazy.map! { |l| sync(l)} else @runner.schema.sync_lazy(lazy) end rescue GraphQL::UnauthorizedError => auth_err @runner.schema.(auth_err) rescue GraphQL::ExecutionError => err err rescue StandardError => stderr begin @selections_step.query.handle_or_reraise(stderr) rescue GraphQL::ExecutionError => ex_err ex_err end end |
#value ⇒ Object
Implement that Lazy API
218 219 220 221 222 223 224 225 |
# File 'lib/graphql/execution/next/field_resolve_step.rb', line 218 def value query = @selections_step.query query.current_trace.begin_execute_field(@field_definition, @arguments, @field_results, query) @field_results = sync(@field_results) query.current_trace.end_execute_field(@field_definition, @arguments, @field_results, query, @field_results) @runner.add_step(self) true end |