Skip to main content

Overview

The Rails::GraphQL::Type class is the foundational object for all GraphQL types in the Rails GraphQL framework. Any schema definition extends from this class. Namespace: Rails::GraphQL Extends:
  • ActiveSupport::Autoload
  • Helpers::WithDirectives
  • Helpers::WithGlobalID
  • Helpers::Registerable

Type Kinds

The following type kinds are available:
KINDS
Array<String>
['Scalar', 'Object', 'Interface', 'Union', 'Enum', 'Input']
Each kind has its own specialized subclass:
  • Type::Scalar - Leaf types representing primitive values
  • Type::Object - Object types with fields
  • Type::Interface - Abstract types implemented by objects
  • Type::Union - Types representing one of several object types
  • Type::Enum - Enumeration types with predefined values
  • Type::Input - Input object types for arguments

Class Attributes

base_object
Boolean
default:"false"
Identifies what methods are available to work as resolvers

Class Methods

base_type

Returns the base type class. Will be one of the classes defined in Type::KINDS. Returns: Class - One of Scalar, Object, Interface, Union, Enum, or Input
User.base_type # => Rails::GraphQL::Type::Object

kind

Return the base type as a symbol. Returns: Symbol - :scalar, :object, :interface, :union, :enum, or :input
User.kind # => :object
String.kind # => :scalar

kind_enum

Return the specific value for the __TypeKind enum of this class. Returns: String - Uppercase kind name
User.kind_enum # => "OBJECT"
String.kind_enum # => "SCALAR"

input_type?

Check if the current type is a valid input type. Returns: Boolean
UserInput.input_type? # => true
User.input_type? # => false

output_type?

Check if the current type is a valid output type. Returns: Boolean
User.output_type? # => true
UserInput.output_type? # => false

leaf_type?

Check if the current type is a leaf output type (Scalar or Enum). Returns: Boolean
String.leaf_type? # => true
User.leaf_type? # => false

operational?

Check if the object is a source of operations (Query, Mutation, Subscription). Returns: Boolean
type.operational? # => false

=~

Check if another type is equivalent or a subclass.
other
Module | Object
required
Type or instance to compare
Returns: Boolean Alias: of_type?
User =~ GraphQL::Type::Object # => true
user_instance.of_type?(User) # => true

decorate

Helper to instantiate the type if necessary.
value
Any
required
Value to potentially decorate
Returns: Decorated value or original value
decorated = User.decorate(user_data)

find_by_gid

Find and optionally instantiate a type by its Global ID.
gid
GlobalID
required
The Global ID object
Returns: Type class or instance
type = Type.find_by_gid(gid)

create!

Dynamically create a new type using the Creator.
from
Schema
required
The schema creating the type
name
String | Symbol
required
Name of the type
superclass
Class
Parent class (defaults to self)
xargs
Hash
Additional options
configure
Block
Configuration block
Returns: New type class
AddressType = Type.create!(schema, :Address, Type::Object) do
  field :street, :string
  field :city, :string
  field :zip_code, :string
end

to_gql_backtrace

Generate a summarized representation for backtraces. Returns: String
User.to_gql_backtrace # => "#<GraphQL::Object User>"

Type Check Methods

Each type kind has a corresponding question method:

Instance Methods

gql_base_type

Delegates to class method base_type. Returns: Base type class

gql_kind

Delegates to class method kind. Returns: Symbol

gql_kind_enum

Delegates to class method kind_enum. Returns: String

gql_input_type?

Delegates to class method input_type?. Returns: Boolean

gql_output_type?

Delegates to class method output_type?. Returns: Boolean

gql_leaf_type?

Delegates to class method leaf_type?. Returns: Boolean

Protected Methods

setup!

Provide setup options for child classes. Called internally during type creation.
kind
Symbol
The kind of type
leaf
Boolean
Whether this is a leaf type
input
Boolean
Whether this is an input type
output
Boolean
Whether this is an output type

valid_token?

Check if a value is a valid parser token.
value
Any
required
Value to check
type
Symbol
default:"to_sym"
Token type to validate against
Returns: Boolean

Example: Defining Custom Types

Object Type

class User < Rails::GraphQL::Type::Object
  desc 'A user in the system'

  field :id, :id, null: false
  field :name, :string, null: false
  field :email, :string
  field :created_at, :datetime, null: false

  field :posts, [Post], null: false do
    def resolve
      object.posts
    end
  end
end

Input Type

class UserInput < Rails::GraphQL::Type::Input
  desc 'Input for creating a user'

  argument :name, :string, null: false
  argument :email, :string, null: false
  argument :password, :string, null: false
end

Enum Type

class UserRole < Rails::GraphQL::Type::Enum
  desc 'User role in the system'

  add :admin, value: 'administrator'
  add :moderator
  add :user
end

Interface Type

class Node < Rails::GraphQL::Type::Interface
  desc 'An object with an ID'

  field :id, :id, null: false
end

class User < Rails::GraphQL::Type::Object
  implements Node

  field :name, :string
end

Scalar Type

class DateTime < Rails::GraphQL::Type::Scalar
  desc 'ISO 8601 datetime string'

  def self.deserialize(value)
    Time.iso8601(value)
  rescue ArgumentError
    raise GraphQL::InvalidValueError
  end

  def self.as_json(value)
    value.iso8601
  end
end

Type Registration

Types are automatically registered in the TypeMap when defined. Each type must have a unique name within its namespace.
# Types are automatically available
schema.find_type(:User) # => User
schema.find_type!(:Post) # => Post

Build docs developers (and LLMs) love