Define abstract types that enable polymorphic fields and shared functionality across objects
Interfaces define a set of fields that multiple object types must implement. They enable polymorphic field resolution and are perfect for representing shared characteristics across different object types.
The Star Wars schema demonstrates interfaces for character polymorphism:
Star Wars Character Interface
interface 'Character' do desc 'A character in the Star Wars Trilogy' field :id, null: false, desc: 'The id of the character' field :name, desc: 'The name of the character' field :friends, 'Character', array: true, desc: 'The friends of the character, or an empty list if they have none' field :appears_in, 'Episode', array: true, desc: 'Which movies they appear in' field :secret_backstory, desc: 'All secrets about their past'end
By default, interface fields are automatically imported as proxies:
object 'Human' do implements 'Character' # All Character fields are automatically available # Add Human-specific fields field :home_planet, :stringendobject 'Droid' do implements 'Character' # All Character fields are automatically available # Add Droid-specific fields field :primary_function, :stringend
Proxy fields delegate resolution to the interface instance, perfect for sharing resolution logic across objects.
For validation-only interfaces, prevent field import:
# Option 1: Abstract interfaceinterface 'Person' do self.abstract = true # Never instantiated field :emailend# Option 2: Per-implementation controlobject 'User' do # Define all fields first field :email field :name # Then declare implementation without importing implements 'Person', import_fields: falseend
When an object implements an interface, all fields are validated for equivalency:
interface 'Character' do field :id, :id, null: false field :name, :stringendobject 'Human' do implements 'Character' # ✓ Valid: matches interface signature field :id, :id, null: false field :name, :string # ✗ Invalid: different nullability # field :id, :id, null: true # Would raise ArgumentErrorend
Fields are checked by equivalency. Mismatched fields raise an ArgumentError.
interface 'Searchable' do field :search_rank, :float def search_rank current_value.calculate_search_score endendobject 'Post' do implements 'Searchable' # Inherits search_rank field and resolutionendobject 'Comment' do implements 'Searchable' # Inherits search_rank field and resolutionend