⚡️ GraphQL Cached
Write a cache layer for each resolver in GraphQL Shield style with Memcached
Dependencies
- Memcached
- GraphQL Middleware
- Your GraphQL Schema
Install
$ yarn add graphql-cached
Usage
graphql-tools
with // Generated resolver types by `@graphql-codegen/cli`
Cache Field
Required
key
:fieldKey
generation function
Optional
lifetime
: How much time to keep the cache (seconds) (default: 10 seconds)serializer
: Preprocess item before storing in cache and after fetching from cache
Configuration
Required
memcached
: Memcached client instancecontextKey
: Key generation function based onContext
Life cycle hooks (Optional)
beforeGet
: Triggered before cache fetchingafterGet
: Triggered after cache fetchingbeforeSave
: Triggered before cache storingafterSave
: Triggered before cache storingonHit
: Triggered when cache hitonMiss
: Triggered when cache miss
Full cache key
{contextKey}${typeName}${fieldName}${fieldKey}
Examples
v1.Admin$User$image$dbf66e27-9bb4-5682-b890-ecf34fe63333
v1.User$Query$user${"where":{"id":"57c5fb3f-a5e7-5b4f-a7ed-ab50281b8222"}}
⚠️ Be careful!
If there are resolvers with different responses for different users, carefully write the key generation function. Items cached for one user may be visible to other users.
Also, if there is a cached item, the existing resolver does not work. If you want to do permission-related logic before cache layer, I recommend using it with graphql-shield.
For more information on how GraphQL Middleware works, check out GraphQL Middleware.
Usage examples
If you have a feature request or a bug, please create a new issue. And also, pull requests are always welcome🙏