Skip to content

Commit 517151b

Browse files
authored
Merge pull request #6 from maxnowack/patch-3
Fix cache key and return value on first call
2 parents 5550c28 + fcead70 commit 517151b

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/cache.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
const hash = require('object-hash');
22

3+
const toSafeObject = obj => JSON.parse(JSON.stringify(obj || {}));
4+
35
function cache(func, options) {
46
return (root, args, context) => {
57
if (!context.resolverCache) {
68
throw new Error('Missing resolverCache property on the Graphql context.');
79
}
810

9-
const key = `${hash(root)}:${hash(args)}`;
11+
const key = `${hash(func)}:${hash(toSafeObject(root))}:${hash(toSafeObject(args))}`;
1012
const executeAndCache = () =>
11-
Promise.resolve(func(root, args, context)).then(value =>
12-
context.resolverCache.set(key, value, options)
13-
);
13+
Promise.resolve(func(root, args, context)).then((value) => {
14+
context.resolverCache.set(key, value, options);
15+
return value;
16+
});
1417

1518
return (
1619
context.resolverCache

0 commit comments

Comments
 (0)