graphql-resolver-cache
graphql-resolver-cache copied to clipboard
Caching for Graphql Resolvers
Graphql resolver cache
Easy wrapper around resolvers to cache results based on root elements and Graphql query arguments. Works best with Apollo Graphql.
Installation
$ npm install graphql-resolver-cache --save
Configuration
Add a cache to your Graphql middleware:
import express from 'express';
import bodyParser from 'body-parser';
import { graphqlExpress } from 'apollo-server-express';
import { LruCache } from 'graphql-resolver-cache';
const myGraphQLSchema = // ... define or import your schema here!
const PORT = 3000;
const app = express();
const resolverCache = new LruCache();
// bodyParser is needed just for POST.
app.use('/graphql', bodyParser.json(), graphqlExpress({
schema: myGraphQLSchema,
context: { resolverCache }
}));
app.listen(PORT);
Wrap your resolver in a cache function:
import { withCache } from 'graphql-resolver-cache';
export default {
User: {
getFriends: withCache((root, args, context) => { /* logic */ }),
},
};