Install and configure Rails GraphQL in your Rails application
Rails GraphQL is distributed as a Ruby gem and integrates seamlessly with Rails applications. This guide covers installation, configuration, and verification.
Rails GraphQL provides several configuration options. Create an initializer to customize behavior:
config/initializers/graphql.rb
Rails::GraphQL.configure do |config| # Enable or disable introspection (disable in production for security) config.enable_introspection = true # Set cache store (defaults to Rails.cache) config.cache = ActiveSupport::Cache::MemoryStore.new # Default response format (:hash or :json) config.default_response_format = :hash # Enable string collector for better performance config.enable_string_collector = trueend
The generated controller includes the GraphQL::Controller module:
app/controllers/graphql_controller.rb
class GraphQLController < ApplicationController include GraphQL::Controller # Skip CSRF token verification for API requests skip_before_action :verify_authenticity_tokenend
Only skip CSRF token verification if your GraphQL endpoint is intended as an API. If you’re using GraphQL with web forms, keep CSRF protection enabled.
Rails GraphQL provides several generators to speed up development:
# Generate a schemarails g graphql:schema# Generate a controllerrails g graphql:controller# Generate an object typerails g graphql:object User# Generate a mutationrails g graphql:mutation CreateUser# Generate a queryrails g graphql:query Users
Run rails g to see all available GraphQL generators.