Skip to main content

Overview

Object types represent a list of named fields, each of which yield a value of a specific type. Objects are the most common output type in GraphQL schemas. Inherits from: Rails::GraphQL::Type Specification: GraphQL Object Type Definition

Class Attributes

field_type
Class
default:"Field::OutputField"
The type of field class used for this type’s fields
valid_field_types
Array<Class>
Valid field types that can be used in object fields:
  • Type::Enum
  • Type::Interface
  • Type::Object
  • Type::Scalar
  • Type::Union

Class Methods

implements(*others, import_fields: true)

Assign one or more interfaces to the object type.
others
Array<Interface>
required
One or more interface types to implement
import_fields
Boolean
default:"true"
Whether to automatically import fields from the interface
class UserType < GraphQL::ObjectType
  implements NodeInterface, TimestampedInterface
end

implements?(interface)

Check if the object implements a given interface.
interface
Interface
required
The interface to check
return
Boolean
Returns true if the object implements the interface
UserType.implements?(NodeInterface) # => true

valid_member?(value)

Check if a given value is a valid member of this object type.
value
Any
required
The value to validate
return
Boolean
Returns true if the value is valid for this object type
UserType.valid_member?(user_instance) # => true

=~(other)

Check if another type is equivalent to this object.
other
Type
required
The type to compare
return
Boolean
Returns true if the other type is equivalent or is an interface this object implements

Included Modules

Helpers::Instantiable
Provides instance creation capabilities
Helpers::WithAssignment
Allows assignment to Ruby classes via assigned_to
Helpers::WithFields
Provides field definition and management methods

Configuration

class UserType < GraphQL::ObjectType
  description "Represents a user in the system"
  
  # Assign to a Ruby class
  self.assigned_to = User
  
  # Implement interfaces
  implements NodeInterface
  
  # Define fields
  field :id, :id, null: false
  field :name, :string
  field :email, :string
end

Special Features

Interface Implementation

When implementing an interface with import_fields: true, all interface fields are automatically copied to the object. Pre-existing fields with the same name must be equivalent.

Field Inheritance

Fields defined in parent object classes are automatically inherited by subclasses.
  • Interface - Define shared field contracts
  • Union - Combine multiple object types
  • Fields - Define object fields

Build docs developers (and LLMs) love