Core philosophy
Rails GraphQL follows Rails conventions rather than fighting against them. Theapp/graphql directory behaves similarly to other Rails directories, and all GraphQL objects are encapsulated in the GraphQL module to avoid naming collisions.
The GraphQL module
Everything in Rails GraphQL lives inside theGraphQL module. This prevents your GraphQL types from colliding with your Rails models and provides a clear namespace:
app/graphql/app_schema.rb
This structure is fully compatible with Zeitwerk, Rails’ code autoloader.
Core components
Rails GraphQL consists of nine crucial components that work together to process GraphQL requests:Schema
A schema is where types meet and organize themselves. It defines what queries, mutations, and subscriptions are available. Think of a schema as its own Rails application - the fields are its routing system, and the types are everything it can respond to.app/graphql/app_schema.rb
- Define query, mutation, and subscription fields
- Support for one or multiple schemas per application
- Namespace isolation for schema separation
- Direct execution:
GraphQL::AppSchema.execute('{ welcome }')
Type
Types are the heart of GraphQL. Almost everything in GraphQL is a type:Object
Complex types with fields
Scalar
Leaf types (String, Int, etc.)
Interface
Shared fields across types
Union
One of several types
Enum
Predefined values
Input
Complex input arguments
Field
Fields are the most important concept in GraphQL. Everything you can access from any GraphQL operation is based on fields.Argument
Arguments allow fields to accept parameters, changing their behavior based on input:Directive
Directives modify behavior at execution or definition time. Rails GraphQL provides four built-in directives:@deprecated- Mark fields as deprecated@skip- Conditionally skip fields@include- Conditionally include fields@specifiedBy- Reference scalar specifications
Framework components
These components are specific to Rails GraphQL and make the magic happen:Type Map
The Type Map is a centralized registry that knows about all schemas, types, and their relationships. It acts as an index for your GraphQL application. What it stores:- All schemas available in your application
- All types each schema has access to
- Aliases to other types
- Mapping between values and their underlying objects
The Type Map uses a namespace → base_class → key structure to organize and retrieve types efficiently.
Request
The Request object is responsible for executing GraphQL operations. It follows a three-step process:Source
Sources bridge your Ruby classes (especially ActiveRecord models) to GraphQL types. They automatically generate GraphQL types and fields based on your models.app/graphql/sources/user_source.rb
- PostgreSQL
- MySQL
- SQLite
Sources are optional but provide a powerful way to quickly expose your ActiveRecord models through GraphQL.
Event
During a request’s lifecycle, several events occur. Events are how the request interacts with your code::attach- When an object is attached to a schema:resolve- When a field is being resolved:prepare- Before field execution:finalize- After field execution
Callback
Callbacks are the counterpart of events. Methods and Procs are turned into callbacks that respond to events:Integration with Rails
Rails GraphQL integrates deeply with Rails features:ActiveRecord integration
Direct connection through Sources:ActionCable integration
Built-in subscription support:Logging
Enhanced Rails logs with GraphQL information:Caching
Integrates with Rails cache:Naming conventions
Rails GraphQL follows specific naming patterns:| Context | Convention | Example |
|---|---|---|
| Ruby classes | PascalCase | GraphQL::User |
| GraphQL types | PascalCase | "User" |
| Field names (Ruby) | snake_case | :user_name |
| Field names (GraphQL) | camelCase | "userName" |
| Arguments (Ruby) | snake_case | :user_id |
| Arguments (GraphQL) | camelCase | "userId" |
Shortcuts
Rails GraphQL provides a::GraphQL module as shortcut to ::Rails::GraphQL:
Next steps
Schemas
Learn how to define and organize schemas
Types
Explore all GraphQL type kinds
Fields
Master field definitions and resolvers
Sources
Bridge ActiveRecord models to GraphQL