GraphQL Ruby
The graphql gem implements the GraphQL Server Specification in Ruby.
Use it to add a GraphQL API to your Ruby or Rails app.
Install the Gem
Get going fast with the graphql gem,
battle-tested and trusted by GitHub, Shopify, Flexport, Chime, and Kickstarter.
# Download the gem:
bundle add graphql
# Setup with Rails:
rails generate graphql:installDefine Your Schema
Describe your application with a GraphQL schema to create a self-documenting, strongly-typed API.
# app/graphql/types/profile_type.rb
class Types::ProfileType < Types::BaseObject
field :id, ID, null: false
field :name, String, null: false
field :avatar, Types::PhotoType
endServe Queries
Provide custom data to clients and extend your API with mutations, subscriptions, streaming responses, and multiplexing.
# app/controllers/graphql_controller.rb
result = MySchema.execute(
params[:query],
variables: params[:variables],
context: { current_user: current_user },
)
render json: resultHarden Your API
Confidently deploy GraphQL with GraphQL-Ruby:
- Testing helpers to validate your system
- Authorization integrates with your app's permission system
- GraphQL::Dataloader optimizes access to data sources
- Complexity limits, timeouts, and rate limits to protect your server resources
- Tracing for integration with your APM or custom usage
- API versioning to roll out changes while preserving client experience
- Persisted queries to guarantee approved API usage
- Caching to serve repeated data requests
Integrate with Client Libraries
graphql-ruby-client provides integration with
Apollo Client,
Relay,
GraphiQL,
urql, or custom JavaScript.
Going Beyond
Customize your GraphQL API:
- Language tooling for manipulating GraphQL documents
- Type system extensions for customizing your schema definition
- Query analysis for ahead-of-time query inspection
