Skip to main content

Overview

Interface types represent a list of named fields and their types. They define a contract that object types can implement, ensuring those objects include specific fields. Inherits from: Rails::GraphQL::Type Specification: GraphQL Interface Type Definition

Class Attributes

field_type
Class
default:"Field::OutputField"
The type of field class used for this interface’s fields
types
Array<Object>
Collection of object types that implement this interface (managed automatically)

Class Methods

type_for(value, *)

Determine which implementing object type is compatible with the provided value.
value
Any
required
The value to find a type for
return
Object | nil
Returns the first compatible object type, or nil if none found
NodeInterface.type_for(user_instance) # => UserType

=~(other)

Check if another type is equivalent to this interface.
other
Type
required
The type to compare
return
Boolean
Returns true if the other type is equivalent or is an object that implements this interface
NodeInterface =~ UserType # => true (if UserType implements NodeInterface)

implemented(object, import_fields: true)

Called automatically when an object type implements this interface. You typically don’t call this directly.
object
Object
required
The object type implementing this interface
import_fields
Boolean
default:"true"
Whether to copy interface fields to the object

Included Modules

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

Configuration

module NodeInterface < GraphQL::InterfaceType
  description "An object with a globally unique ID"
  
  field :id, :id, null: false do
    desc "The globally unique identifier"
  end
end

class UserType < GraphQL::ObjectType
  implements NodeInterface
  
  # The :id field is automatically imported
  field :name, :string
end

Special Features

Abstract Interfaces

When an interface is marked as abstract, import_fields is automatically set to false in the implemented method.

Validation

Interfaces skip field type validation during the validation phase, as fields are copied and interface field types might be broken due to namespaces.

Type Resolution

Interfaces don’t implement valid_output? or serialization methods directly. The object types that implement the interface handle validation and serialization.

Usage Patterns

Multiple Interfaces

class UserType < GraphQL::ObjectType
  implements NodeInterface, TimestampedInterface, AuditableInterface
end

Selective Field Import

class CustomType < GraphQL::ObjectType
  # Don't import fields, define them manually
  implements NodeInterface, import_fields: false
  
  field :id, :id, null: false do
    # Custom implementation
  end
end
  • Object - Implement interfaces
  • Union - Alternative to interfaces for polymorphism

Build docs developers (and LLMs) love