Skip to content
A caching plugin for graphql-ruby
Branch: master
Clone or download
Latest commit 290c329 Mar 7, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github Add contributing guidlines and pr template May 10, 2018
bin First pass refactor to plugin Jul 9, 2018
gemfiles Add dependency support for graphql-ruby 1.9 Feb 28, 2019
lib/graphql Refactor for shorter parameter lists Mar 29, 2019
spec Add feature specs to cover caching connections Mar 29, 2019
test_schema Fix scope issue with Schema name Jul 9, 2018
.gitignore Setup basic Appraisals file May 22, 2018
.rspec Creates initial gem with `bundle gem graphql-cache` May 8, 2018
.rubocop.yml Add ruby version to rubocop config May 22, 2018
.travis.yml Remove explicit bundle install from test bootstrap Feb 28, 2019
.yardopts Expand API documentation May 23, 2018
Appraisals Add dependency support for graphql-ruby 1.9 Feb 28, 2019 Creates initial gem with `bundle gem graphql-cache` May 8, 2018 Add contributing guidlines and pr template May 10, 2018
Gemfile Creates initial gem with `bundle gem graphql-cache` May 8, 2018
LICENSE.txt Creates initial gem with `bundle gem graphql-cache` May 8, 2018 Updated url May 16, 2019
Rakefile Creates initial gem with `bundle gem graphql-cache` May 8, 2018
graphql-cache.gemspec Update gem summary and description to match github Mar 5, 2019
test_schema.rb Supress db logs during creation and bootstrap Jul 9, 2018

GraphQL Cache

Gem Version Build Status Test Coverage Maintainability Stack Share

A custom caching plugin for graphql-ruby


  • Provide resolver-level caching for GraphQL APIs written in ruby
  • Configurable to work with or without Rails
  • API Documentation


At StackShare we've been rolling out graphql-ruby for several of our new features and found ourselves in need of a caching solution. We could have simply used Rails.cache in our resolvers, but this creates very verbose types or resolver classes. It also means that each and every resolver must define it's own expiration and key. GraphQL Cache solves that problem by integrating caching functionality into the graphql-ruby resolution process making caching transparent on most fields except for a metadata flag denoting the field as cached. More details on our motivation for creating this here.


Add this line to your application's Gemfile:

gem 'graphql-cache'

And then execute:

$ bundle

Or install it yourself as:

$ gem install graphql-cache


  1. Use GraphQL Cache as a plugin in your schema.
class MySchema < GraphQL::Schema
  query Types::Query

  use GraphQL::Cache
  1. Add the custom caching field class to your base object class. This adds the cache metadata key when defining fields.
module Types
  class Base < GraphQL::Schema::Object
    field_class GraphQL::Cache::Field

Also note that if you want access to the cache keyword param in interface fields, the field_class directive must be added to your base interface module as well


GraphQL Cache can be configured in an initializer:

# config/initializers/graphql_cache.rb

GraphQL::Cache.configure do |config|
  config.namespace = 'GraphQL::Cache' # Cache key prefix for keys generated by graphql-cache
  config.cache     = Rails.cache      # The cache object to use for caching
  config.logger    = Rails.logger     # Logger to receive cache-related log messages
  config.expiry    = 5400             # 90 minutes (in seconds)
  config.force     = false            # Cache override, when true no caching takes place


Any object, list, or connection field can be cached by simply adding cache: true to the field definition:

field :calculated_field, Int, cache: true

Custom Expirations

By default all keys will have an expiration of GraphQL::Cache.expiry which defaults to 90 minutes. If you want to set a field-specific expiration time pass a hash to the cache parameter like this:

field :calculated_field, Int, cache: { expiry: 10800 } # expires key after 180 minutes

Custom cache keys

GraphQL Cache generates a cache key using the context of a query during execution. A custom key can be included to implement versioned caching as well. By providing a :key value to the cache config hash on a field definition. For example, to use a custom method that returns the cache key for an object use:

field :calculated_field, Int, cache: { key: :custom_cache_key }

With this configuration the cache key used for this resolved value will use the result of the method custom_cache_key called on the parent object.

Forcing the cache

It is possible to force graphql-cache to resolve and write all cached fields to cache regardless of the presence of a given key in the cache store. This will effectively "renew" any existing cached expirations and warm any that don't exist. To use forced caching, add a value to :force_cache in the query context:

MySchema.execute('{ company(id: 123) { cachedField }}', context: { force_cache: true })

This will resolve all cached fields using the field's resolver and write them to cache without first reading the value at their respective cache keys. This is useful for structured cache warming strategies where the cache expiration needs to be updated when a warming query is made.


After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install.


Bug reports and pull requests are welcome on GitHub at This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.


The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the graphql-cache project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

You can’t perform that action at this time.