const hash = require('object-hash');

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

function cache(func, options) {
  return (root, args, context) => {
    if (!context.resolverCache) {
      throw new Error('Missing resolverCache property on the Graphql context.');
    }

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

    return (
      context.resolverCache
        .get(key)
        .then(value => (value ? value : executeAndCache()))
        // Error
        .catch(() => executeAndCache())
    );
  };
}

module.exports = cache;

@@ -288,16 +288,20 @@
 ;%0A    %7D%0A
+    
 %0A    con
@@ -308,16 +308,79 @@
 st key =
+ options.key %0A      ? options.key(root, args, context) %0A      :
  %60$%7Bhash
@@ -445,16 +445,17 @@
 gs))%7D%60;%0A
+%0A
     cons

Allow to set custom cache key through options