Skip to content

Commit ff9cd94

Browse files
committed
feature/cache: Add initial cache function
1 parent d049ffd commit ff9cd94

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/cache.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const hash = require('object-hash');
2+
3+
function cache(func) {
4+
return (root, args, context) => {
5+
if (!context.resolverCache) {
6+
throw new Error('Missing resolverCache property on the Graphql context.');
7+
}
8+
9+
const key = `${hash(root)}:${hash(args)}`;
10+
return (
11+
context.resolverCache
12+
.get(key)
13+
.then(result => (result ? result : func(root, args, context)))
14+
// Error
15+
.catch(() => func(root, args, context))
16+
);
17+
};
18+
}
19+
20+
module.exports = cache;

0 commit comments

Comments
 (0)