All Projects → stackshareio → Graphql Cache

stackshareio / Graphql Cache

Licence: mit
A caching plugin for graphql-ruby

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Graphql Cache

Quell
Quell is an easy-to-use, lightweight JavaScript library providing a client- and server-side caching solution for GraphQL. Use Quell to prevent redundant client-side API requests and to minimize costly server-side response latency.
Stars: ✭ 90 (-71.25%)
Mutual labels:  graphql, caching
Typeorm Loader
A database-aware data-loader for use with GraphQL and TypeORM.
Stars: ✭ 86 (-72.52%)
Mutual labels:  graphql, caching
Vulcain
Fast and idiomatic client-driven REST APIs.
Stars: ✭ 3,190 (+919.17%)
Mutual labels:  graphql
Graphql Starter
💥 Monorepo template (seed project) pre-configured with GraphQL API, PostgreSQL, React, Relay, and Material UI.
Stars: ✭ 3,377 (+978.91%)
Mutual labels:  graphql
Graffiti
Minimalistic GraphQL framework
Stars: ✭ 306 (-2.24%)
Mutual labels:  graphql
Graphcms Examples
Example projects to help you get started with GraphCMS
Stars: ✭ 295 (-5.75%)
Mutual labels:  graphql
Nest Angular
NestJS, Angular 6, Server Side Rendering (Angular Universal), GraphQL, JWT (JSON Web Tokens) and Facebook/Twitter/Google Authentication, Mongoose, MongoDB, Webpack, TypeScript
Stars: ✭ 307 (-1.92%)
Mutual labels:  graphql
Jobs
Come and join the Entria team
Stars: ✭ 292 (-6.71%)
Mutual labels:  graphql
Gatsby Woocommerce Themes
⚡ A Gatsby Theme for WooCommerce E-commerce site Gatsby WooCommerce WordPress
Stars: ✭ 306 (-2.24%)
Mutual labels:  graphql
Github Wrapped
Take a look back at all the contributions you as an individual made to the open-source community
Stars: ✭ 304 (-2.88%)
Mutual labels:  graphql
Metaphysics
Artsy's GraphQL API
Stars: ✭ 305 (-2.56%)
Mutual labels:  graphql
Graphql Wp
GraphQL endpoint for WordPress
Stars: ✭ 303 (-3.19%)
Mutual labels:  graphql
Simonw
https://simonwillison.net/2020/Jul/10/self-updating-profile-readme/
Stars: ✭ 297 (-5.11%)
Mutual labels:  graphql
Foshttpcache
Integrate your PHP application with your HTTP caching proxy
Stars: ✭ 308 (-1.6%)
Mutual labels:  caching
Graphql Apis
📜 A collective list of public GraphQL APIs
Stars: ✭ 3,525 (+1026.2%)
Mutual labels:  graphql
Altair
✨⚡️ A beautiful feature-rich GraphQL Client for all platforms.
Stars: ✭ 3,827 (+1122.68%)
Mutual labels:  graphql
Laravel Varnish
Making Varnish and Laravel play nice together
Stars: ✭ 291 (-7.03%)
Mutual labels:  caching
Angular Slickgrid
Angular-Slickgrid is a wrapper of the lightning fast & customizable SlickGrid datagrid, it also includes multiple Styling Themes
Stars: ✭ 298 (-4.79%)
Mutual labels:  graphql
Graphql Rate Limit
Add Rate Limiting To Your GraphQL Resolvers 💂‍♀️
Stars: ✭ 306 (-2.24%)
Mutual labels:  graphql
Saturday Night Works
Takip ettiğim kaynaklardaki örneklere ait çalışmalar yer alır.
Stars: ✭ 312 (-0.32%)
Mutual labels:  graphql

GraphQL Cache

Gem Version Build Status Test Coverage Maintainability Stack Share

A custom caching plugin for graphql-ruby

Goals

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

Why?

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.

Installation

Add this line to your application's Gemfile:

gem 'graphql-cache'

And then execute:

$ bundle

Or install it yourself as:

$ gem install graphql-cache

Setup

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

  use GraphQL::Cache
end
  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
  end
end

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

Configuration

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)
end

Usage

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.

Development

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.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/stackshareio/graphql-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.

License

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..

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].