Lighthouse GraphQL Rails
A framework for serving GraphQL from Ruby on Rails
๐ SDL First
Describe your API in the GraphQL Schema Definition Language and add behaviour with server-side directives, instead of a resolver class for every field.
๐ค๏ธ Rails Friendly
Built on graphql-ruby for an existing Rails app. Your models, scopes, validations, Pundit policies and Rails.cache keep doing their jobs.
โก Optimized for Active Record
Relationships and pages load in batches through the Dataloader, so a list of 100 posts with their authors and comments is a handful of queries.
Boilerplate-free schema definition
Define your schema without any boilerplate, using the GraphQL Schema Definition Language.
type User {
name: String!
posts: [Post!]! @hasMany(type: PAGINATOR)
}
type Post {
title: String!
author: User! @belongsTo
}
type Query {
me: User @guard
posts(orderBy: [PostOrderByClause!] @orderBy): [Post!]! @paginate(defaultCount: 10)
}
type Mutation {
createPost(input: CreatePostInput! @spread): Post!
@guard
@canModel(ability: "create")
@create
@inject(context: "current_user.id", name: "author_id")
}
Get going in minutes
Add the gem, run the installer, and query the schema it generates.
bundle add lighthouse-graphql
bin/rails generate lighthouse:install
curl localhost:3000/graphql -H 'Content-Type: application/json' \
-d '{"query": "{ users { data { id name } paginatorInfo { total } } }"}'
Ruby where there is real logic
A Query or Mutation field without a resolving directive resolves by convention to a graphql-ruby class: Mutation.publishPost to Mutations::PublishPost.
class Mutations::PublishPost < GraphQL::Schema::Mutation
def resolve(id:)
Post.find(id).tap { |post| post.update!(published: true) }
end
end
Built for small apps and large ones
Nested mutations, @whereConditions, model validations and Dry::Validation contracts, the @can* family over Pundit, @cache and @cacheControl, Apollo Federation โ and, when you need them, a schema cache, persisted queries and an operation allowlist.