Skip to content

Intellicode/graphql-resolver-cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Graphql resolver cache

Greenkeeper badge

Maintenance Status NPM version Build Status Dependency Status Coverage Status Code Climate BCH compliance

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 */ }),
  },
};