Class: GraphQL::Schema::Resolver

Inherits:
Object
  • Object
show all
Extended by:
Member::BaseDSLMethods, Member::HasArguments, Member::HasDirectives, Member::HasPath, Member::HasValidators
Includes:
Member::GraphQLTypeNames, Member::HasDataloader, Member::HasPath
Defined in:
lib/graphql/schema/resolver.rb,
lib/graphql/schema/resolver/has_payload_type.rb
more...

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.
  • #object and #context accessors 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 Also:

  • for a concrete subclass of `Resolver`.
  • `Resolver` is a replacement for `GraphQL::Function`

Direct Known Subclasses

Mutation, Subscription

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

Attributes included from Member::BaseDSLMethods

#default_graphql_name, #graphql_name

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Member::BaseDSLMethods

comment, default_relay, description, introspection, introspection?, mutation, name, visible?

Methods included from Member::HasArguments

add_argument, all_argument_definitions, any_arguments?, argument, argument_class, arguments_statically_coercible?, coerce_arguments, own_arguments, remove_argument, validate_directive_argument

Methods included from Member::HasValidators

validates, validators

Methods included from Member::HasPath

path

Methods included from Member::HasDirectives

add_directive, directive, directives, get_directives, inherited, remove_directive, remove_directive

Methods included from Member::HasDataloader

#dataload, #dataload_association, #dataload_record, #dataloader

Constructor Details

#initialize(object:, context:, field:) ⇒ Resolver

Returns a new instance of Resolver.

Parameters:

[View source]

35
36
37
38
39
40
41
42
43
44
45
# File 'lib/graphql/schema/resolver.rb', line 35

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
end

Instance Attribute Details

#contextGraphQL::Query::Context (readonly)


51
52
53
# File 'lib/graphql/schema/resolver.rb', line 51

def context
  @context
end

#fieldGraphQL::Schema::Field (readonly)


54
55
56
# File 'lib/graphql/schema/resolver.rb', line 54

def field
  @field
end

#objectObject (readonly)

Returns The application object this field is being resolved on.

Returns:

  • (Object)

    The application object this field is being resolved on


48
49
50
# File 'lib/graphql/schema/resolver.rb', line 48

def object
  @object
end

Class Method Details

.all_field_argument_definitionsObject

[View source]

231
232
233
# File 'lib/graphql/schema/resolver.rb', line 231

def all_field_argument_definitions
  all_argument_definitions
end

.any_field_arguments?Boolean

Returns:

[View source]

223
224
225
# File 'lib/graphql/schema/resolver.rb', line 223

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 Also:

  • for the signature
[View source]

372
373
374
375
376
# File 'lib/graphql/schema/resolver.rb', line 372

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.
  super(*args, from_resolver: true, **kwargs)
end

.broadcastable(new_broadcastable) ⇒ Object

[View source]

309
310
311
# File 'lib/graphql/schema/resolver.rb', line 309

def broadcastable(new_broadcastable)
  @broadcastable = new_broadcastable
end

.broadcastable?Boolean?

Returns:

[View source]

314
315
316
317
318
319
320
# File 'lib/graphql/schema/resolver.rb', line 314

def broadcastable?
  if defined?(@broadcastable)
    @broadcastable
  else
    (superclass.respond_to?(:broadcastable?) ? superclass.broadcastable? : nil)
  end
end

.complexity(new_complexity = nil) ⇒ Integer, Proc

Specifies the complexity of the field. Defaults to 1

Returns:

  • (Integer, Proc)
[View source]

302
303
304
305
306
307
# File 'lib/graphql/schema/resolver.rb', line 302

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) ⇒ Integer?

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)

    The default_page_size assigned to fields that use this resolver

[View source]

347
348
349
350
351
352
353
354
355
356
357
# File 'lib/graphql/schema/resolver.rb', line 347

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 class

  • options (Hash)

    Optional extension options

[View source]

381
382
383
384
# File 'lib/graphql/schema/resolver.rb', line 381

def extension(extension, **options)
  @own_extensions ||= []
  @own_extensions << {extension => options}
end

.extensionsObject

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.

[View source]

387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/graphql/schema/resolver.rb', line 387

def extensions
  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 Also:

  • {GraphQL{GraphQL::Schema{GraphQL::Schema::Field{GraphQL::Schema::Field#extras}
[View source]

246
247
248
249
250
251
252
# File 'lib/graphql/schema/resolver.rb', line 246

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

[View source]

219
220
221
# File 'lib/graphql/schema/resolver.rb', line 219

def field_arguments(context = GraphQL::Query::NullContext.instance)
  arguments(context)
end

.get_field_argument(name, context = GraphQL::Query::NullContext.instance) ⇒ Object

[View source]

227
228
229
# File 'lib/graphql/schema/resolver.rb', line 227

def get_field_argument(name, context = GraphQL::Query::NullContext.instance)
  get_argument(name, context)
end

.has_default_page_size?Boolean

Returns true if this resolver or a superclass has an assigned default_page_size.

Returns:

  • (Boolean)

    true if this resolver or a superclass has an assigned default_page_size

[View source]

360
361
362
# File 'lib/graphql/schema/resolver.rb', line 360

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 true if this resolver or a superclass has an assigned max_page_size.

Returns:

  • (Boolean)

    true if this resolver or a superclass has an assigned max_page_size

[View source]

339
340
341
# File 'lib/graphql/schema/resolver.rb', line 339

def has_max_page_size?
  (!!defined?(@max_page_size)) || (superclass.respond_to?(:has_max_page_size?) && superclass.has_max_page_size?)
end

.max_page_size(new_max_page_size = NOT_CONFIGURED) ⇒ Integer?

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)

    The max_page_size assigned to fields that use this resolver

[View source]

326
327
328
329
330
331
332
333
334
335
336
# File 'lib/graphql/schema/resolver.rb', line 326

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.

Parameters:

  • allow_null (Boolean) (defaults to: nil)

    Whether or not the response can be null

See Also:

  • which sets the return type of this field and accepts a `null:` option
[View source]

259
260
261
262
263
264
265
# File 'lib/graphql/schema/resolver.rb', line 259

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) ⇒ Symbol

Default :resolve set below.

Returns:

  • (Symbol)

    The method to call on instances of this object to resolve the field

[View source]

237
238
239
240
241
242
# File 'lib/graphql/schema/resolver.rb', line 237

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

[View source]

267
268
269
270
271
272
273
# File 'lib/graphql/schema/resolver.rb', line 267

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) ⇒ Class

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) (defaults to: nil)

    If a type definition class is provided, it will be used as the return type of the field

  • null (true, false) (defaults to: nil)

    Whether or not the field may return nil

Returns:

  • (Class)

    The type which this field returns.

[View source]

282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/graphql/schema/resolver.rb', line 282

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_exprObject

A non-normalized type configuration, without null applied

[View source]

365
366
367
# File 'lib/graphql/schema/resolver.rb', line 365

def type_expr
  @type_expr || (superclass.respond_to?(:type_expr) ? superclass.type_expr : nil)
end

Instance Method Details

#argumentsObject

[View source]

56
57
58
# File 'lib/graphql/schema/resolver.rb', line 56

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, early_return_data

Called after arguments are loaded, but before resolving.

Override it to check everything before calling the mutation.

Parameters:

  • inputs (Hash)

    The input arguments

Returns:

  • (Boolean, early_return_data)

    If false, execution will stop (and early_return_data will be returned instead, if present.)

Raises:

[View source]

150
151
152
153
154
# File 'lib/graphql/schema/resolver.rb', line 150

def authorized?(**inputs)
  arg_owner = @field # || self.class
  args = context.types.arguments(arg_owner)
  authorize_arguments(args, inputs)
end

#call_resolve(args_hash) ⇒ Object

[View source]

115
116
117
118
119
120
121
# File 'lib/graphql/schema/resolver.rb', line 115

def call_resolve(args_hash)
  if !args_hash.empty?
    public_send(self.class.resolve_method, **args_hash)
  else
    public_send(self.class.resolve_method)
  end
end

#ready?(**args) ⇒ Boolean, early_return_data

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

Returns:

  • (Boolean, early_return_data)

    If false, execution will stop (and early_return_data will be returned instead, if present.)

Raises:

[View source]

139
140
141
# File 'lib/graphql/schema/resolver.rb', line 139

def ready?(**args)
  true
end

#resolve(**args) ⇒ Object

Do the work. Everything happens here.

Returns:

  • (Object)

    An object corresponding to the return type

Raises:

[View source]

125
126
127
# File 'lib/graphql/schema/resolver.rb', line 125

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 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 is actually called by the runtime, it does some preparation and then eventually calls the user-defined #resolve method.

[View source]

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
109
110
111
112
# File 'lib/graphql/schema/resolver.rb', line 64

def resolve_with_support(**args)
  # 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
        raw_authorized_val = if !loaded_args.empty?
          authorized?(**loaded_args)
        else
          authorized?
        end
        context.query.after_lazy(raw_authorized_val) do |authorized_val|
          # If the `authorized?` returned two values, `false, early_return`,
          # then use the early return value instead of continuing
          if authorized_val.is_a?(Array)
            authorized_result, early_return = authorized_val
            if authorized_result == false
              early_return
            else
              raise "Unexpected result from #authorized? (expected `true`, `false` or `[false, {...}]`): [#{authorized_result.inspect}, #{early_return.inspect}]"
            end
          elsif authorized_val
            # 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 GraphQL::Schema::Resolver.{Schema{Schema.unauthorized_object}.

Any value returned here will be used instead of of the loaded object.

Parameters:

[View source]

162
163
164
# File 'lib/graphql/schema/resolver.rb', line 162

def unauthorized_object(err)
  raise err
end