Module: GraphQL::Schema::Member::HasArguments::ArgumentObjectLoader

Included in:
InputObject
Defined in:
lib/graphql/schema/member/has_arguments.rb

Overview

-- @requires_ancestor: Kernel

Instance Method Summary collapse

Instance Method Details

#authorize_application_object(argument, id, context, loaded_application_object) ⇒ Object



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
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
# File 'lib/graphql/schema/member/has_arguments.rb', line 428

def authorize_application_object(argument, id, context, loaded_application_object)
  context.query.after_lazy(loaded_application_object) do |application_object|
    if application_object.nil?
      err = GraphQL::LoadApplicationObjectFailedError.new(context: context, argument: argument, id: id, object: application_object) #: GraphQL::RuntimeError
      application_object = load_application_object_failed(err)
    end
    # Double-check that the located object is actually of this type
    # (Don't want to allow arbitrary access to objects this way)
    if application_object.nil?
      nil
    else
      arg_loads_type = argument.loads
      maybe_lazy_resolve_type = context.schema.resolve_type(arg_loads_type, application_object, context)
      context.query.after_lazy(maybe_lazy_resolve_type) do |resolve_type_result|
        if resolve_type_result.is_a?(Array) && resolve_type_result.size == 2
          application_object_type, application_object = resolve_type_result
        else
          application_object_type = resolve_type_result
          # application_object is already assigned
        end

        passes_possible_types_check = if context.types.loadable?(arg_loads_type, context)
          if arg_loads_type.kind.abstract?
            # This union/interface is used in `loads:` but not otherwise visible to this query
            context.types.loadable_possible_types(arg_loads_type, context).include?(application_object_type)
          else
            arg_loads_type == application_object_type
          end
        else
          context.types.possible_types(arg_loads_type).include?(application_object_type)
        end
        if !passes_possible_types_check
          err = GraphQL::LoadApplicationObjectFailedError.new(context: context, argument: argument, id: id, object: application_object)
          application_object = load_application_object_failed(err)
        end

        if application_object.nil?
          nil
        else
          # This object was loaded successfully
          # and resolved to the right type,
          # now apply the `.authorized?` class method if there is one
          context.query.after_lazy(application_object_type.authorized?(application_object, context)) do |authed|
            if authed
              application_object
            else
              err = GraphQL::UnauthorizedError.new(
                object: application_object,
                type: application_object_type,
                context: context,
              )
              if self.respond_to?(:unauthorized_object)
                err.set_backtrace(caller)
                loader = self #: untyped
                loader.unauthorized_object(err)
              else
                raise err
              end
            end
          end
        end
      end
    end
  end
end

#load_and_authorize_application_object(argument, id, context) ⇒ Object



423
424
425
426
# File 'lib/graphql/schema/member/has_arguments.rb', line 423

def load_and_authorize_application_object(argument, id, context)
  loaded_application_object = load_application_object(argument, id, context)
  authorize_application_object(argument, id, context, loaded_application_object)
end

#load_application_object(argument, id, context) ⇒ Object



415
416
417
418
419
420
421
# File 'lib/graphql/schema/member/has_arguments.rb', line 415

def load_application_object(argument, id, context)
  # See if any object can be found for this ID
  if id.nil?
    return nil
  end
  object_from_id(argument.loads, id, context)
end

#load_application_object_failed(err) ⇒ Object

Called when an argument's loads: configuration fails to fetch an application object. By default, this method raises the given error, but you can override it to handle failures differently.

API: public

Parameters

  • err (GraphQL::LoadApplicationObjectFailedError) — The error that occurred

Returns

  • Object, nil — If a value is returned, it will be used instead of the failed load

:call-seq:

load_application_object_failed(GraphQL::LoadApplicationObjectFailedError err) -> Object | nil


509
510
511
# File 'lib/graphql/schema/member/has_arguments.rb', line 509

def load_application_object_failed(err)
  raise err
end

#object_from_id(type, id, context) ⇒ Object

Look up the corresponding object for a provided ID. By default, it uses Relay-style Schema.object_from_id, override this to find objects another way.

Parameters

  • type (Class, Module) — A GraphQL type definition
  • id (String) — A client-provided to look up
  • context (GraphQL::Query::Context) — the current context

:call-seq:

object_from_id(Class | Module type, String id, GraphQL::Query::Context context)


411
412
413
# File 'lib/graphql/schema/member/has_arguments.rb', line 411

def object_from_id(type, id, context)
  context.schema.object_from_id(id, context)
end