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 36
def authorized_new(object, context)
  context.schema.after_lazy(authorized?(object, context)) do |is_authorized|
    if is_authorized
      self.new(object, context)
    else
      raise GraphQL::UnauthorizedError.new(object: object, type: self, context: context)
    end
  end
end
fields() click to toggle source

Include legacy-style interfaces, too

Calls superclass method
# File lib/graphql/schema/object.rb, line 74
def fields
  all_fields = super
  interfaces.each do |int|
    if int.is_a?(GraphQL::InterfaceType)
      int_f = {}
      int.fields.each do |name, legacy_field|
        int_f[name] = field_class.from_options(name, field: legacy_field)
      end
      all_fields = int_f.merge(all_fields)
    end
  end
  all_fields
end
implements(*new_interfaces) click to toggle source
# File lib/graphql/schema/object.rb, line 53
def implements(*new_interfaces)
  new_interfaces.each do |int|
    if int.is_a?(Module)
      # Include the methods here,
      # `.fields` will use the inheritance chain
      # to find inherited fields
      include(int)
    end
  end
  own_interfaces.concat(new_interfaces)
end
interfaces() click to toggle source
# File lib/graphql/schema/object.rb, line 65
def interfaces
  own_interfaces + (superclass <= GraphQL::Schema::Object ? superclass.interfaces : [])
end
kind() click to toggle source
# File lib/graphql/schema/object.rb, line 107
def kind
  GraphQL::TypeKinds::OBJECT
end
new(object, context) click to toggle source
# File lib/graphql/schema/object.rb, line 47
def initialize(object, context)
  @object = object
  @context = context
end
own_interfaces() click to toggle source
# File lib/graphql/schema/object.rb, line 69
def own_interfaces
  @own_interfaces ||= []
end
to_graphql() click to toggle source

@return [GraphQL::ObjectType]

# File lib/graphql/schema/object.rb, line 89
def to_graphql
  obj_type = GraphQL::ObjectType.new
  obj_type.name = graphql_name
  obj_type.description = description
  obj_type.interfaces = interfaces
  obj_type.introspection = introspection
  obj_type.mutation = mutation

  fields.each do |field_name, field_inst|
    field_defn = field_inst.to_graphql
    obj_type.fields[field_defn.name] = field_defn
  end

  obj_type.metadata[:type_class] = self

  obj_type
end