1.1.0 • Published 6 years ago

graphql-resolver-cache v1.1.0

Weekly downloads
10
License
MIT
Repository
github
Last release
6 years ago

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

6 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago