Before you begin
Make sure you have:- Rails 6.0 or higher
- Ruby 2.6.3 or higher
- An existing Rails application
Install Rails GraphQL
Add the gem to your Gemfile and run the Rails generator:
The generator creates:
app/graphql/app_schema.rb- Your main GraphQL schemaapp/controllers/graphql_controller.rb- Controller to handle requests- Route configuration for
/graphqlendpoint
Define your schema
Open This creates a GraphQL field named
app/graphql/app_schema.rb and add a simple field that returns a greeting:app/graphql/app_schema.rb
welcome that returns the string “Hello World!” when queried.What you just built
Congratulations! You’ve created your first GraphQL API with Rails GraphQL. Here’s what happened:- Installed the framework - The generator set up the necessary files and configuration
- Defined a schema - Created a GraphQL schema with a single query field
- Executed a query - Made a request and received a JSON response
Add more fields
Let’s expand your schema with additional fields:app/graphql/app_schema.rb
Next steps
Architecture
Learn how Rails GraphQL integrates with Rails
Schemas
Understand schemas and how to organize your GraphQL API
Fields
Deep dive into defining and configuring fields
Types
Explore objects, interfaces, unions, and more