class GraphQL::Schema::Object

Attributes

context[R]

@return [GraphQL::Query::Context] the context instance for this query

object[R]

@return [Object] the application object this type is wrapping

Public Class Methods

authorized_new(object, context) click to toggle source

Make a new instance of this type if the auth check passes, otherwise, raise an error.

Probably only the framework should call this method.

This might return a {GraphQL::Execution::Lazy} if the user-provided ‘.authorized?` hook returns some lazy value (like a Promise).

The reason that the auth check is in this wrapper method instead of {.new} is because of how it might return a Promise. It would be weird if ‘.new` returned a promise; It would be a headache to try to maintain Promise-y state inside a {Schema::Object} instance. So, hopefully this wrapper method will do the job.

@param object [Object] The thing wrapped by this object @param context [GraphQL::Query::Context] @return [GraphQL::Schema::Object, GraphQL::Execution::Lazy] @raise [GraphQL::UnauthorizedError] if the user-provided hook returns ‘false`

# File lib/graphql/schema/object.rb, line 50
def authorized_new(object, context)
  trace_payload = { context: context, type: self, object: object, path: context[:current_path] }

  maybe_lazy_auth_val = context.query.trace("authorized", trace_payload) do
    begin
      authorized?(object, context)
    rescue GraphQL::UnauthorizedError => err
      context.schema.unauthorized_object(err)
    rescue StandardError => err
      context.query.handle_or_reraise(err)
    end
  end

  auth_val = if context.schema.lazy?(maybe_lazy_auth_val)
    GraphQL::Execution::Lazy.new do
      context.query.trace("authorized_lazy", trace_payload) do
        context.schema.sync_lazy(maybe_lazy_auth_val)
      end
    end
  else
    maybe_lazy_auth_val
  end

  context.schema.after_lazy(auth_val) do |is_authorized|
    if is_authorized
      self.new(object, context)
    else
      # It failed the authorization check, so go to the schema's authorized object hook
      err = GraphQL::UnauthorizedError.new(object: object, type: self, context: context)
      # If a new value was returned, wrap that instead of the original value
      begin
        new_obj = context.schema.unauthorized_object(err)
        if new_obj
          self.new(new_obj, context)
        else
          nil
        end
      end
    end
  end
end
const_missing(name) click to toggle source

Set up a type-specific invalid null error to use when this object’s non-null fields wrongly return ‘nil`. It should help with debugging and bug tracker integrations.

Calls superclass method
# File lib/graphql/schema/object.rb, line 101
def const_missing(name)
  if name == :InvalidNullError
    custom_err_class = GraphQL::InvalidNullError.subclass_for(self)
    const_set(:InvalidNullError, custom_err_class)
    custom_err_class
  else
    super
  end
end
kind() click to toggle source
# File lib/graphql/schema/object.rb, line 111
def kind
  GraphQL::TypeKinds::OBJECT
end
new(object, context) click to toggle source
# File lib/graphql/schema/object.rb, line 93
def initialize(object, context)
  @object = object
  @context = context
end

Public Instance Methods

dataloader() click to toggle source

@return [GraphQL::Dataloader]

# File lib/graphql/schema/object.rb, line 18
def dataloader
  context.dataloader
end
raw_value(obj) click to toggle source

Call this in a field method to return a value that should be returned to the client without any further handling by GraphQL.

# File lib/graphql/schema/object.rb, line 24
def raw_value(obj)
  GraphQL::Execution::Interpreter::RawValue.new(obj)
end