Skip to content

Commit 07665b8

Browse files
authored
Merge pull request #10 from matteodem/patch-1
Allow to set custom cache key through options
2 parents 3e579e0 + 91a3d06 commit 07665b8

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/cache.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ const hash = require('object-hash');
22

33
const toSafeObject = obj => JSON.parse(JSON.stringify(obj || {}));
44

5-
function cache(func, options) {
5+
function cache(func, options = {}) {
66
return (root, args, context) => {
77
if (!context.resolverCache) {
88
throw new Error('Missing resolverCache property on the Graphql context.');
99
}
10+
11+
const key = options.key
12+
? options.key(root, args, context)
13+
: `${hash(func)}:${hash(toSafeObject(root))}:${hash(toSafeObject(args))}`;
1014

11-
const key = `${hash(func)}:${hash(toSafeObject(root))}:${hash(toSafeObject(args))}`;
1215
const executeAndCache = () =>
1316
Promise.resolve(func(root, args, context)).then((value) => {
1417
context.resolverCache.set(key, value, options);

0 commit comments

Comments
 (0)