Class: GraphQL::Schema::Resolver
- Inherits:
-
Object
- Object
- GraphQL::Schema::Resolver
- Extended by:
- Member::BaseDSLMethods, Member::HasArguments, Member::HasAuthorization, Member::HasAuthorization::ClassConfigured, Member::HasDeprecationReason, Member::HasDirectives, Member::HasPath, Member::HasValidators
- Defined in:
- lib/graphql/schema/resolver.rb,
lib/graphql/schema/resolver/has_payload_type.rb
Overview
A class-based container for field configuration and resolution logic. It supports:
- Arguments, via
.argument(...)helper, which will be applied to the field. - Return type, via
.type(..., null: ...), which will be applied to the field. - Description, via
.description(...), which will be applied to the field - Comment, via
.comment(...), which will be applied to the field - Resolution, via
#resolve(**args)method, which will be called to resolve the field. #objectand#contextaccessors for use during#resolve.
Resolvers can be attached with the resolver: option in a field(...) call.
A resolver's configuration may be overridden with other keywords in the field(...) call.
See GraphQL::Schema::Mutation for a concrete subclass of Resolver.
Resolver is a replacement for the former GraphQL::Function API.
Direct Known Subclasses
Defined Under Namespace
Modules: HasPayloadType
Constant Summary
Constants included from Member::HasArguments
Member::HasArguments::NO_ARGUMENTS
Constants included from EmptyObjects
EmptyObjects::EMPTY_ARRAY, EmptyObjects::EMPTY_HASH
Constants included from Member::GraphQLTypeNames
Member::GraphQLTypeNames::Boolean, Member::GraphQLTypeNames::ID, Member::GraphQLTypeNames::Int
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns.
-
#exec_index ⇒ Object
Returns the value of attribute exec_index.
-
#exec_result ⇒ Object
Returns the value of attribute exec_result.
-
#field ⇒ Object
readonly
Returns.
-
#field_resolve_step ⇒ Object
Returns the value of attribute field_resolve_step.
-
#object ⇒ Object
Returns.
-
#prepared_arguments ⇒ Object
writeonly
Sets the attribute prepared_arguments.
-
#raw_arguments ⇒ Object
Returns the value of attribute raw_arguments.
Attributes included from Member::BaseDSLMethods
#default_graphql_name, #graphql_name
Attributes included from Member::HasDeprecationReason
Class Method Summary collapse
- .all_field_argument_definitions ⇒ Object
- .any_field_arguments? ⇒ Boolean
-
.argument(*args, **kwargs, &block) ⇒ Object
Add an argument to this field's signature, but also add some preparation hook methods which will be used for this argument See GraphQL::Schema::Argument for the signature.
- .authorizes?(context) ⇒ Boolean
- .broadcastable(new_broadcastable) ⇒ Object
-
.broadcastable? ⇒ Boolean
Returns.
-
.complexity(new_complexity = nil) ⇒ Object
Specifies the complexity of the field.
-
.default_page_size(new_default_page_size = NOT_CONFIGURED) ⇒ Object
Get or set the
default_page_size:which will be configured for fields using this resolver (nilmeans "unlimited default page size".). -
.extension(extension, **options) ⇒ Object
Registers new extension.
-
.extensions ⇒ Object
:nodoc:.
-
.extras(new_extras = nil) ⇒ Object
Additional info injected into resolve See GraphQL::Schema::Field#extras.
- .field_arguments(context = GraphQL::Query::NullContext.instance) ⇒ Object
- .get_field_argument(name, context = GraphQL::Query::NullContext.instance) ⇒ Object
-
.has_default_page_size? ⇒ Boolean
Returns.
-
.has_max_page_size? ⇒ Boolean
Returns.
- .inherited(child_class) ⇒ Object
-
.max_page_size(new_max_page_size = NOT_CONFIGURED) ⇒ Object
Get or set the
max_page_size:which will be configured for fields using this resolver (nilmeans "unlimited max page size".). -
.null(allow_null = nil) ⇒ Object
If
true(default), then the return type for this resolver will be nullable. -
.resolve_method(new_method = nil) ⇒ Object
Default
:resolveset below. - .resolver_method(new_method_name = nil) ⇒ Object
-
.type(new_type = nil, null: nil) ⇒ Object
Call this method to get the return type of the field, or use it as a configuration method to assign a return type instead of generating one.
-
.type_expr ⇒ Object
A non-normalized type configuration, without
nullapplied.
Instance Method Summary collapse
- #arguments ⇒ Object
-
#authorized?(**inputs) ⇒ Boolean
Called after arguments are loaded, but before resolving.
- #call ⇒ Object
-
#call_resolve(args_hash) ⇒ Object
:nodoc:.
-
#initialize(object:, context:, field:) ⇒ Resolver
constructor
Parameters.
-
#ready?(**args) ⇒ Boolean
Called before arguments are prepared.
-
#resolve(**args) ⇒ Object
Do the work.
-
#resolve_with_support(**args) ⇒ Object
This method is actually called by the runtime, it does some preparation and then eventually calls the user-defined
#resolvemethod. -
#unauthorized_object(err) ⇒ Object
Called when an object loaded by
loads:fails the.authorized?check for its resolved GraphQL object type.
Methods included from Member::BaseDSLMethods
comment, default_relay?, description, introspection, introspection?, mutation, visible?
Methods included from FindInheritedValue
Methods included from Member::HasArguments
add_argument, all_argument_definitions, any_arguments?, argument, argument_class, arguments_statically_coercible?, coerce_arguments, extended, included, own_arguments, remove_argument, validate_directive_argument
Methods included from Member::HasAuthorization
Methods included from Member::HasAuthorization::ClassConfigured
Methods included from Member::HasValidators
extended, validates, validators
Methods included from Member::HasPath
Methods included from Member::HasDirectives
add_directive, directive, directives, extended, get_directives, inherited, remove_directive, remove_directive
Methods included from Member::HasDeprecationReason
Methods included from Member::HasDataloader
#dataload, #dataload_all, #dataload_all_associations, #dataload_all_records, #dataload_association, #dataload_record, #dataloader
Constructor Details
#initialize(object:, context:, field:) ⇒ Resolver
Parameters
object(Object) — The application object that this field is being resolved oncontext(GraphQL::Query::Context)field(GraphQL::Schema::Field)
:call-seq:
initialize(Object object:, GraphQL::Query::Context context:, GraphQL::Schema::Field field:)
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/graphql/schema/resolver.rb', line 45 def initialize(object:, context:, field:) @object = object @context = context @field = field # Since this hash is constantly rebuilt, cache it for this call @arguments_by_keyword = {} context.types.arguments(self.class).each do |arg| @arguments_by_keyword[arg.keyword] = arg end @prepared_arguments = nil #: Hash[Symbol, untyped]? end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns
GraphQL::Query::Context
67 68 69 |
# File 'lib/graphql/schema/resolver.rb', line 67 def context @context end |
#exec_index ⇒ Object
Returns the value of attribute exec_index.
57 58 59 |
# File 'lib/graphql/schema/resolver.rb', line 57 def exec_index @exec_index end |
#exec_result ⇒ Object
Returns the value of attribute exec_result.
57 58 59 |
# File 'lib/graphql/schema/resolver.rb', line 57 def exec_result @exec_result end |
#field ⇒ Object (readonly)
Returns
GraphQL::Schema::Field
72 73 74 |
# File 'lib/graphql/schema/resolver.rb', line 72 def field @field end |
#field_resolve_step ⇒ Object
Returns the value of attribute field_resolve_step.
57 58 59 |
# File 'lib/graphql/schema/resolver.rb', line 57 def field_resolve_step @field_resolve_step end |
#object ⇒ Object
Returns
Object— The application object this field is being resolved on
62 63 64 |
# File 'lib/graphql/schema/resolver.rb', line 62 def object @object end |
#prepared_arguments=(value) ⇒ Object (writeonly)
Sets the attribute prepared_arguments
74 75 76 |
# File 'lib/graphql/schema/resolver.rb', line 74 def prepared_arguments=(value) @prepared_arguments = value end |
#raw_arguments ⇒ Object
Returns the value of attribute raw_arguments.
57 58 59 |
# File 'lib/graphql/schema/resolver.rb', line 57 def raw_arguments @raw_arguments end |
Class Method Details
.all_field_argument_definitions ⇒ Object
354 355 356 |
# File 'lib/graphql/schema/resolver.rb', line 354 def all_field_argument_definitions all_argument_definitions end |
.any_field_arguments? ⇒ Boolean
346 347 348 |
# File 'lib/graphql/schema/resolver.rb', line 346 def any_field_arguments? any_arguments? end |
.argument(*args, **kwargs, &block) ⇒ Object
Add an argument to this field's signature, but also add some preparation hook methods which will be used for this argument See GraphQL::Schema::Argument for the signature
555 556 557 558 559 560 |
# File 'lib/graphql/schema/resolver.rb', line 555 def argument(*args, **kwargs, &block) # Use `from_resolver: true` to short-circuit the InputObject's own `loads:` implementation # so that we can support `#load_{x}` methods below. args = args #: untyped super(*args, from_resolver: true, **kwargs) end |
.authorizes?(context) ⇒ Boolean
269 270 271 |
# File 'lib/graphql/schema/resolver.rb', line 269 def self.(context) self.instance_method(:authorized?).owner != GraphQL::Schema::Resolver || super end |
.broadcastable(new_broadcastable) ⇒ Object
459 460 461 |
# File 'lib/graphql/schema/resolver.rb', line 459 def broadcastable(new_broadcastable) @broadcastable = new_broadcastable end |
.broadcastable? ⇒ Boolean
Returns
Boolean, nil
:call-seq:
broadcastable?() -> bool | nil
469 470 471 472 473 474 475 |
# File 'lib/graphql/schema/resolver.rb', line 469 def broadcastable? if defined?(@broadcastable) @broadcastable else (superclass.respond_to?(:broadcastable?) ? superclass.broadcastable? : nil) end end |
.complexity(new_complexity = nil) ⇒ Object
Specifies the complexity of the field. Defaults to 1
Returns
Integer, Proc
:call-seq:
complexity(new_complexity) -> Integer | Proc
452 453 454 455 456 457 |
# File 'lib/graphql/schema/resolver.rb', line 452 def complexity(new_complexity = nil) if new_complexity @complexity = new_complexity end @complexity || (superclass.respond_to?(:complexity) ? superclass.complexity : 1) end |
.default_page_size(new_default_page_size = NOT_CONFIGURED) ⇒ Object
Get or set the default_page_size: which will be configured for fields using this resolver
(nil means "unlimited default page size".)
Parameters
default_page_size(Integer, nil) — Set a new value
Returns
Integer, nil— Thedefault_page_sizeassigned to fields that use this resolver
:call-seq:
default_page_size(new_default_page_size) -> Integer | nil
525 526 527 528 529 530 531 532 533 534 535 |
# File 'lib/graphql/schema/resolver.rb', line 525 def default_page_size(new_default_page_size = NOT_CONFIGURED) if new_default_page_size != NOT_CONFIGURED @default_page_size = new_default_page_size elsif defined?(@default_page_size) @default_page_size elsif superclass.respond_to?(:default_page_size) superclass.default_page_size else nil end end |
.extension(extension, **options) ⇒ Object
Registers new extension
Parameters
extension(Class) — Extension classoptions(Hash) — Optional extension options
:call-seq:
extension(Class extension, Hash **options)
571 572 573 574 |
# File 'lib/graphql/schema/resolver.rb', line 571 def extension(extension, **) @own_extensions ||= [] @own_extensions << {extension => } end |
.extensions ⇒ Object
:nodoc:
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 |
# File 'lib/graphql/schema/resolver.rb', line 576 def extensions # :nodoc: own_exts = @own_extensions # Jump through some hoops to avoid creating arrays when we don't actually need them if superclass.respond_to?(:extensions) s_exts = superclass.extensions if own_exts if !s_exts.empty? own_exts + s_exts else own_exts end else s_exts end else own_exts || EMPTY_ARRAY end end |
.extras(new_extras = nil) ⇒ Object
Additional info injected into resolve See GraphQL::Schema::Field#extras
375 376 377 378 379 380 381 |
# File 'lib/graphql/schema/resolver.rb', line 375 def extras(new_extras = nil) if new_extras @own_extras = new_extras end own_extras = @own_extras || [] own_extras + (superclass.respond_to?(:extras) ? superclass.extras : []) end |
.field_arguments(context = GraphQL::Query::NullContext.instance) ⇒ Object
342 343 344 |
# File 'lib/graphql/schema/resolver.rb', line 342 def field_arguments(context = GraphQL::Query::NullContext.instance) arguments(context) end |
.get_field_argument(name, context = GraphQL::Query::NullContext.instance) ⇒ Object
350 351 352 |
# File 'lib/graphql/schema/resolver.rb', line 350 def get_field_argument(name, context = GraphQL::Query::NullContext.instance) get_argument(name, context) end |
.has_default_page_size? ⇒ Boolean
Returns
Boolean—trueif this resolver or a superclass has an assigneddefault_page_size
:call-seq:
has_default_page_size?() -> bool
543 544 545 |
# File 'lib/graphql/schema/resolver.rb', line 543 def has_default_page_size? (!!defined?(@default_page_size)) || (superclass.respond_to?(:has_default_page_size?) && superclass.has_default_page_size?) end |
.has_max_page_size? ⇒ Boolean
Returns
Boolean—trueif this resolver or a superclass has an assignedmax_page_size
:call-seq:
has_max_page_size?() -> bool
508 509 510 |
# File 'lib/graphql/schema/resolver.rb', line 508 def has_max_page_size? (!!defined?(@max_page_size)) || (superclass.respond_to?(:has_max_page_size?) && superclass.has_max_page_size?) end |
.inherited(child_class) ⇒ Object
595 596 597 598 |
# File 'lib/graphql/schema/resolver.rb', line 595 def inherited(child_class) child_class.description(description) super end |
.max_page_size(new_max_page_size = NOT_CONFIGURED) ⇒ Object
Get or set the max_page_size: which will be configured for fields using this resolver
(nil means "unlimited max page size".)
Parameters
max_page_size(Integer, nil) — Set a new value
Returns
Integer, nil— Themax_page_sizeassigned to fields that use this resolver
:call-seq:
max_page_size(new_max_page_size) -> Integer | nil
490 491 492 493 494 495 496 497 498 499 500 |
# File 'lib/graphql/schema/resolver.rb', line 490 def max_page_size(new_max_page_size = NOT_CONFIGURED) if new_max_page_size != NOT_CONFIGURED @max_page_size = new_max_page_size elsif defined?(@max_page_size) @max_page_size elsif superclass.respond_to?(:max_page_size) superclass.max_page_size else nil end end |
.null(allow_null = nil) ⇒ Object
If true (default), then the return type for this resolver will be nullable.
If false, then the return type is non-null.
See type which sets the return type of this field and accepts a null: option
Parameters
allow_null(Boolean) — Whether or not the response can be null
:call-seq:
null(bool allow_null)
394 395 396 397 398 399 400 |
# File 'lib/graphql/schema/resolver.rb', line 394 def null(allow_null = nil) if !allow_null.nil? @null = allow_null end @null.nil? ? (superclass.respond_to?(:null) ? superclass.null : true) : @null end |
.resolve_method(new_method = nil) ⇒ Object
Default :resolve set below.
Returns
Symbol— The method to call on instances of this object to resolve the field
:call-seq:
resolve_method(new_method) -> Symbol
366 367 368 369 370 371 |
# File 'lib/graphql/schema/resolver.rb', line 366 def resolve_method(new_method = nil) if new_method @resolve_method = new_method end @resolve_method || (superclass.respond_to?(:resolve_method) ? superclass.resolve_method : :resolve) end |
.resolver_method(new_method_name = nil) ⇒ Object
402 403 404 405 406 407 408 |
# File 'lib/graphql/schema/resolver.rb', line 402 def resolver_method(new_method_name = nil) if new_method_name @resolver_method = new_method_name else @resolver_method || :resolve_with_support end end |
.type(new_type = nil, null: nil) ⇒ Object
Call this method to get the return type of the field, or use it as a configuration method to assign a return type instead of generating one. TODO unify with null
Parameters
new_type(Class, Array<Class>, nil) — If a type definition class is provided, it will be used as the return type of the fieldnull(true, false) — Whether or not the field may returnnil
Returns
Class— The type which this field returns.
:call-seq:
type(Class | Array[Class] | nil new_type, true | false null:) -> Class
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 |
# File 'lib/graphql/schema/resolver.rb', line 426 def type(new_type = nil, null: nil) if new_type if null.nil? raise ArgumentError, "required argument `null:` is missing" end @type_expr = new_type @null = null else if type_expr GraphQL::Schema::Member::BuildType.parse_type(type_expr, null: self.null) elsif superclass.respond_to?(:type) superclass.type else nil end end end |
.type_expr ⇒ Object
A non-normalized type configuration, without null applied
548 549 550 |
# File 'lib/graphql/schema/resolver.rb', line 548 def type_expr @type_expr || (superclass.respond_to?(:type_expr) ? superclass.type_expr : nil) end |
Instance Method Details
#arguments ⇒ Object
142 143 144 |
# File 'lib/graphql/schema/resolver.rb', line 142 def arguments @prepared_arguments || raise("Arguments have not been prepared yet, still waiting for #load_arguments to resolve. (Call `.arguments` later in the code.)") end |
#authorized?(**inputs) ⇒ Boolean
Called after arguments are loaded, but before resolving.
Override it to check everything before calling the mutation.
Parameters
inputs(Hash) — The input arguments
Raises
GraphQL::ExecutionError— To add an error to the responseGraphQL::UnauthorizedError— To signal an authorization failure
Returns
Boolean, early_return_data— Iffalse, execution will stop (andearly_return_datawill be returned instead, if present.)
:call-seq:
authorized?(Hash **inputs) -> bool | early_return_data
263 264 265 266 267 |
# File 'lib/graphql/schema/resolver.rb', line 263 def (**inputs) arg_owner = @field # || self.class args = context.types.arguments(arg_owner) (args, inputs) end |
#call ⇒ Object
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 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 |
# File 'lib/graphql/schema/resolver.rb', line 76 def call q = context.query trace_objs = [object] q.current_trace.begin_execute_field(field, @prepared_arguments, trace_objs, q) is_ready = ready?(**@prepared_arguments) runner = @field_resolve_step.runner if runner.resolves_lazies && runner.schema.lazy?(is_ready) is_ready, new_return_value = runner.schema.sync_lazy(is_ready) end if is_ready.is_a?(Array) is_ready, new_return_value = is_ready if is_ready != false raise "Unexpected result from #ready? (expected `true`, `false` or `[false, {...}]`): [#{is_ready.inspect}, #{new_return_value.inspect}]" else new_return_value end end if is_ready begin is_authed, new_return_value = (**@prepared_arguments) rescue GraphQL::UnauthorizedError => err new_return_value = q.schema.(err) is_authed = false end end if runner.resolves_lazies && runner.schema.lazy?(is_authed) is_authed, new_return_value = runner.schema.sync_lazy(is_authed) end result = if is_authed Schema::Validator.validate!(self.class.validators, object, context, @prepared_arguments, as: @field) call_resolve(@prepared_arguments) elsif new_return_value.nil? err = UnauthorizedFieldError.new(object: object, type: @field_resolve_step.parent_type, context: context, field: @field) context.schema.(err) else new_return_value end q = context.query q.current_trace.end_execute_field(field, @prepared_arguments, trace_objs, q, [result]) exec_result[exec_index] = result rescue GraphQL::UnauthorizedError => auth_err exec_result[exec_index] = begin context.schema.(auth_err) rescue GraphQL::ExecutionError => exec_err exec_err end rescue GraphQL::RuntimeError => err exec_result[exec_index] = err rescue StandardError => stderr exec_result[exec_index] = begin context.query.handle_or_reraise(stderr, field: @field, arguments: @prepared_arguments, object: object) rescue GraphQL::ExecutionError => ex_err ex_err end ensure field_pending_steps = field_resolve_step.pending_steps field_pending_steps.delete(self) if field_pending_steps.size == 0 && field_resolve_step.field_results field_resolve_step.runner.add_step(field_resolve_step) end end |
#call_resolve(args_hash) ⇒ Object
:nodoc:
199 200 201 202 203 204 205 |
# File 'lib/graphql/schema/resolver.rb', line 199 def call_resolve(args_hash) # :nodoc: if !args_hash.empty? public_send(self.class.resolve_method, **args_hash) else public_send(self.class.resolve_method) end end |
#ready?(**args) ⇒ Boolean
Called before arguments are prepared. Implement this hook to make checks before doing any work.
If it returns a lazy object (like a promise), it will be synced by GraphQL (but the resulting value won't be used).
Parameters
args(Hash) — The input arguments, if there are any
Raises
GraphQL::ExecutionError— To add an error to the responseGraphQL::UnauthorizedError— To signal an authorization failure
Returns
Boolean, early_return_data— Iffalse, execution will stop (andearly_return_datawill be returned instead, if present.)
:call-seq:
ready?(Hash **args) -> bool | early_return_data
240 241 242 |
# File 'lib/graphql/schema/resolver.rb', line 240 def ready?(**args) true end |
#resolve(**args) ⇒ Object
Do the work. Everything happens here.
Returns
Object— An object corresponding to the return type
:call-seq:
resolve(**args) -> Object
215 216 217 |
# File 'lib/graphql/schema/resolver.rb', line 215 def resolve(**args) raise GraphQL::RequiredImplementationMissingError, "#{self.class.name}#resolve should execute the field's logic" end |
#resolve_with_support(**args) ⇒ Object
This method is actually called by the runtime,
it does some preparation and then eventually calls
the user-defined #resolve method.
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 |
# File 'lib/graphql/schema/resolver.rb', line 149 def resolve_with_support(**args) # :nodoc: # First call the ready? hook which may raise raw_ready_val = if !args.empty? ready?(**args) else ready? end context.query.after_lazy(raw_ready_val) do |ready_val| if ready_val.is_a?(Array) is_ready, ready_early_return = ready_val if is_ready != false raise "Unexpected result from #ready? (expected `true`, `false` or `[false, {...}]`): [#{is_ready.inspect}, #{ready_early_return.inspect}]" else ready_early_return end elsif ready_val # Then call each prepare hook, which may return a different value # for that argument, or may return a lazy object load_arguments_val = load_arguments(args) context.query.after_lazy(load_arguments_val) do |loaded_args| @prepared_arguments = loaded_args Schema::Validator.validate!(self.class.validators, object, context, loaded_args, as: @field) # Then call `authorized?`, which may raise or may return a lazy object = if !loaded_args.empty? (**loaded_args) else end context.query.after_lazy() do || # If the `authorized?` returned two values, `false, early_return`, # then use the early return value instead of continuing if .is_a?(Array) , early_return = if == false early_return else raise "Unexpected result from #authorized? (expected `true`, `false` or `[false, {...}]`): [#{.inspect}, #{early_return.inspect}]" end elsif # Finally, all the hooks have passed, so resolve it call_resolve(loaded_args) else raise GraphQL::UnauthorizedFieldError.new(context: context, object: object, type: field.owner, field: field) end end end end end end |
#unauthorized_object(err) ⇒ Object
Called when an object loaded by loads: fails the .authorized? check for its resolved GraphQL object type.
By default, the error is re-raised and passed along to Schema.unauthorized_object.
Any value returned here will be used instead of of the loaded object.
Parameters
err(GraphQL::UnauthorizedError)
:call-seq:
(GraphQL::UnauthorizedError err)
285 286 287 |
# File 'lib/graphql/schema/resolver.rb', line 285 def (err) raise err end |