This gem is to cache the json result, instead of resolved object.
Add this line to your application's Gemfile:
gem 'graphql-result_cache'
And then execute:
$ bundle
Or install it yourself as:
$ gem install graphql-result_cache
- Use
GraphQL::ResultCache
as a plugin in your schema. Only for implementations usinggraphql < 1.10.0
.
class MySchema < GraphQL::Schema
mutation Types::MutationType
query Types::QueryType
use GraphQL::ResultCache
end
- Add the custom field class to accept
result_cache
metadata.
module Types
class BaseObject < GraphQL::Schema::Object
field_class GraphQL::ResultCache::Field
end
end
- Config the fields which need to be cached.
There are two approaches available to config the caching for the field.
Using currently supported GraphQL gem Field Extension
interface or Field Instrument
interface
which is no longer supported as of graphql 1.10.0
.
field :theme, Types::ThemeType, null: false, extensions: [GraphQL::ResultCache::FieldExtension]
Only to be used for implementations where graphql < 1.10.0
.
field :theme, Types::ThemeType, null: false, result_cache: true
- Wrap query result with
GraphQL::ResultCache::Result
.
class GraphqlController < ApplicationController
def execute
# ...
result = if params[:_json]
multiple_execute(params[:_json], context: context)
else
execute_query(context: context)
end
render json: GraphQL::ResultCache::Result.new(result)
end
end
The if
condition can be either a Symbol or a Proc.
field :theme, Types::ThemeType, null: false, do
extension GraphQL::ResultCache::FieldExtension, if: :published?
end
field :theme, Types::ThemeType, null: false, result_cache: { if: :published? }
By default, GraphQL::ResultCache
will generate a cache key combining the field path, arguments and object.
But you can customize the object clause by specify the key
option.
field :theme, Types::ThemeType, null: false, do
extension GraphQL::ResultCache::FieldExtension, key: :theme_cache_key
end
field :theme, Types::ThemeType, null: false, result_cache: { key: :theme_cache_key }
The key
can be either a Symbol or a Proc.
An after_process
callback can be provided, eg. when some dynamic values need to be amended after cached result applied.
field :theme, Types::ThemeType, null: false, do
extension GraphQL::ResultCache::FieldExtension, after_process: :amend_dynamic_attributes
end
field :theme, Types::ThemeType, null: false, result_cache: { after_process: :amend_dynamic_attributes }
def amend_dynamic_attributes(theme_node)
theme_node.merge! used_count: object.theme.used_count
end
GraphQL::ResultCache
can be configured in initializer.
# config/initializers/graphql/result_cache.rb
GraphQL::ResultCache.configure do |config|
config.namespace = "GraphQL:Result" # Cache key namespace
config.expires_in = 1.hour # Expire time for the cache, default to 1.hour
config.client_hash = -> { Rails.cache.read(:deploy_version) } # GraphQL client package hash key, used in cache key generation, default to nil
config.except = ->(ctx) { !ctx[:result_cacheable] } # Exception rule, skip the cache while evaluated as true, default to nil
config.cache = Rails.cache # The cache object, default to Rails.cache in Rails
config.logger = Rails.logger # The Logger, default to Rails.logger in Rails
end
When using with introspection, you need to assign custom introspection to avoid getting the nullable type for a non-null type.
class MySchema < GraphQL::Schema
introspection ::GraphQL::ResultCache::Introspection
end
After checking out the repo, run bin/setup
to install dependencies. 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
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/saharaying/graphql-result_cache. 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.
Everyone interacting in the GraphQL::ResultCache project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.