@envelop/response-cache 3.0.0-alpha-7c8ceda.0 → 3.0.0-alpha-cadd145.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.defaultGetDocumentString = exports.useCacheDocumentString = void 0;
4
+ const graphql_1 = require("graphql");
5
+ const documentStringByDocument = new WeakMap();
6
+ function useCacheDocumentString() {
7
+ return {
8
+ onParse({ params: { source } }) {
9
+ return function onParseEnd({ result }) {
10
+ if (result != null && !(result instanceof Error)) {
11
+ documentStringByDocument.set(result, source.toString());
12
+ }
13
+ };
14
+ },
15
+ };
16
+ }
17
+ exports.useCacheDocumentString = useCacheDocumentString;
18
+ function defaultGetDocumentString(executionArgs) {
19
+ let documentString = documentStringByDocument.get(executionArgs.document);
20
+ if (documentString == null) {
21
+ documentString = (0, graphql_1.print)(executionArgs.document);
22
+ documentStringByDocument.set(executionArgs.document, documentString);
23
+ }
24
+ return documentString;
25
+ }
26
+ exports.defaultGetDocumentString = defaultGetDocumentString;
package/cjs/plugin.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useResponseCache = exports.defaultGetDocumentString = exports.defaultShouldCacheResult = exports.defaultBuildResponseCacheKey = void 0;
3
+ exports.useResponseCache = exports.defaultShouldCacheResult = exports.defaultBuildResponseCacheKey = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const core_1 = require("@envelop/core");
6
6
  const utils_1 = require("@graphql-tools/utils");
@@ -8,6 +8,7 @@ const graphql_1 = require("graphql");
8
8
  const fast_json_stable_stringify_1 = tslib_1.__importDefault(require("fast-json-stable-stringify"));
9
9
  const in_memory_cache_js_1 = require("./in-memory-cache.js");
10
10
  const hashSHA256_js_1 = require("./hashSHA256.js");
11
+ const cache_document_str_js_1 = require("./cache-document-str.js");
11
12
  const contextSymbol = Symbol('responseCache');
12
13
  /**
13
14
  * Default function used for building the response cache key.
@@ -40,17 +41,7 @@ const defaultShouldCacheResult = (params) => {
40
41
  return true;
41
42
  };
42
43
  exports.defaultShouldCacheResult = defaultShouldCacheResult;
43
- const documentStringByDocument = new WeakMap();
44
- function defaultGetDocumentString(executionArgs) {
45
- let documentString = documentStringByDocument.get(executionArgs.document);
46
- if (!documentString) {
47
- documentString = (0, graphql_1.print)(executionArgs.document);
48
- documentStringByDocument.set(executionArgs.document, documentString);
49
- }
50
- return documentString;
51
- }
52
- exports.defaultGetDocumentString = defaultGetDocumentString;
53
- function useResponseCache({ cache = (0, in_memory_cache_js_1.createInMemoryCache)(), ttl: globalTtl = Infinity, session = () => null, enabled, ignoredTypes = [], ttlPerType = {}, ttlPerSchemaCoordinate = {}, idFields = ['id'], invalidateViaMutation = true, buildResponseCacheKey = exports.defaultBuildResponseCacheKey, getDocumentString = defaultGetDocumentString, shouldCacheResult = exports.defaultShouldCacheResult,
44
+ function useResponseCache({ cache = (0, in_memory_cache_js_1.createInMemoryCache)(), ttl: globalTtl = Infinity, session = () => null, enabled, ignoredTypes = [], ttlPerType = {}, ttlPerSchemaCoordinate = {}, idFields = ['id'], invalidateViaMutation = true, buildResponseCacheKey = exports.defaultBuildResponseCacheKey, getDocumentString = cache_document_str_js_1.defaultGetDocumentString, shouldCacheResult = exports.defaultShouldCacheResult,
54
45
  // eslint-disable-next-line dot-notation
55
46
  includeExtensionMetadata = typeof process !== 'undefined' ? process.env['NODE_ENV'] === 'development' : false, } = {}) {
56
47
  const appliedTransform = Symbol('responseCache.appliedTransform');
@@ -58,6 +49,11 @@ includeExtensionMetadata = typeof process !== 'undefined' ? process.env['NODE_EN
58
49
  // never cache Introspections
59
50
  ttlPerSchemaCoordinate = { 'Query.__schema': 0, ...ttlPerSchemaCoordinate };
60
51
  return {
52
+ onPluginInit({ addPlugin }) {
53
+ if (getDocumentString === cache_document_str_js_1.defaultGetDocumentString) {
54
+ addPlugin((0, cache_document_str_js_1.useCacheDocumentString)());
55
+ }
56
+ },
61
57
  onSchemaChange({ schema, replaceSchema }) {
62
58
  var _a;
63
59
  // @ts-expect-error See https://github.com/graphql/graphql-js/pull/3511 - remove this comments once merged
@@ -71,13 +67,6 @@ includeExtensionMetadata = typeof process !== 'undefined' ? process.env['NODE_EN
71
67
  };
72
68
  replaceSchema(patchedSchema);
73
69
  },
74
- onParse({ params: { source } }) {
75
- return function onParseEnd({ result }) {
76
- if (result != null && !(result instanceof Error)) {
77
- documentStringByDocument.set(result, source.toString());
78
- }
79
- };
80
- },
81
70
  async onExecute(ctx) {
82
71
  var _a;
83
72
  const identifier = new Map();
@@ -121,8 +110,9 @@ includeExtensionMetadata = typeof process !== 'undefined' ? process.env['NODE_EN
121
110
  }
122
111
  const operationId = await buildResponseCacheKey({
123
112
  documentString: getDocumentString(ctx.args),
113
+ variableValues: ctx.args.variableValues,
114
+ operationName: ctx.args.operationName,
124
115
  sessionId: session(ctx.args.contextValue),
125
- ...ctx.args,
126
116
  });
127
117
  if (((_a = enabled === null || enabled === void 0 ? void 0 : enabled(ctx.args.contextValue)) !== null && _a !== void 0 ? _a : true) === true) {
128
118
  const cachedResponse = await cache.get(operationId);
@@ -0,0 +1,21 @@
1
+ import { print } from 'graphql';
2
+ const documentStringByDocument = new WeakMap();
3
+ export function useCacheDocumentString() {
4
+ return {
5
+ onParse({ params: { source } }) {
6
+ return function onParseEnd({ result }) {
7
+ if (result != null && !(result instanceof Error)) {
8
+ documentStringByDocument.set(result, source.toString());
9
+ }
10
+ };
11
+ },
12
+ };
13
+ }
14
+ export function defaultGetDocumentString(executionArgs) {
15
+ let documentString = documentStringByDocument.get(executionArgs.document);
16
+ if (documentString == null) {
17
+ documentString = print(executionArgs.document);
18
+ documentStringByDocument.set(executionArgs.document, documentString);
19
+ }
20
+ return documentString;
21
+ }
package/esm/plugin.js CHANGED
@@ -1,9 +1,10 @@
1
1
  import { isAsyncIterable } from '@envelop/core';
2
2
  import { MapperKind, mapSchema } from '@graphql-tools/utils';
3
- import { visit, TypeInfo, visitWithTypeInfo, defaultFieldResolver, print, } from 'graphql';
3
+ import { visit, TypeInfo, visitWithTypeInfo, defaultFieldResolver, } from 'graphql';
4
4
  import jsonStableStringify from 'fast-json-stable-stringify';
5
5
  import { createInMemoryCache } from './in-memory-cache.js';
6
6
  import { hashSHA256 } from './hashSHA256.js';
7
+ import { defaultGetDocumentString, useCacheDocumentString } from './cache-document-str.js';
7
8
  const contextSymbol = Symbol('responseCache');
8
9
  /**
9
10
  * Default function used for building the response cache key.
@@ -34,15 +35,6 @@ export const defaultShouldCacheResult = (params) => {
34
35
  }
35
36
  return true;
36
37
  };
37
- const documentStringByDocument = new WeakMap();
38
- export function defaultGetDocumentString(executionArgs) {
39
- let documentString = documentStringByDocument.get(executionArgs.document);
40
- if (!documentString) {
41
- documentString = print(executionArgs.document);
42
- documentStringByDocument.set(executionArgs.document, documentString);
43
- }
44
- return documentString;
45
- }
46
38
  export function useResponseCache({ cache = createInMemoryCache(), ttl: globalTtl = Infinity, session = () => null, enabled, ignoredTypes = [], ttlPerType = {}, ttlPerSchemaCoordinate = {}, idFields = ['id'], invalidateViaMutation = true, buildResponseCacheKey = defaultBuildResponseCacheKey, getDocumentString = defaultGetDocumentString, shouldCacheResult = defaultShouldCacheResult,
47
39
  // eslint-disable-next-line dot-notation
48
40
  includeExtensionMetadata = typeof process !== 'undefined' ? process.env['NODE_ENV'] === 'development' : false, } = {}) {
@@ -51,6 +43,11 @@ includeExtensionMetadata = typeof process !== 'undefined' ? process.env['NODE_EN
51
43
  // never cache Introspections
52
44
  ttlPerSchemaCoordinate = { 'Query.__schema': 0, ...ttlPerSchemaCoordinate };
53
45
  return {
46
+ onPluginInit({ addPlugin }) {
47
+ if (getDocumentString === defaultGetDocumentString) {
48
+ addPlugin(useCacheDocumentString());
49
+ }
50
+ },
54
51
  onSchemaChange({ schema, replaceSchema }) {
55
52
  var _a;
56
53
  // @ts-expect-error See https://github.com/graphql/graphql-js/pull/3511 - remove this comments once merged
@@ -64,13 +61,6 @@ includeExtensionMetadata = typeof process !== 'undefined' ? process.env['NODE_EN
64
61
  };
65
62
  replaceSchema(patchedSchema);
66
63
  },
67
- onParse({ params: { source } }) {
68
- return function onParseEnd({ result }) {
69
- if (result != null && !(result instanceof Error)) {
70
- documentStringByDocument.set(result, source.toString());
71
- }
72
- };
73
- },
74
64
  async onExecute(ctx) {
75
65
  var _a;
76
66
  const identifier = new Map();
@@ -114,8 +104,9 @@ includeExtensionMetadata = typeof process !== 'undefined' ? process.env['NODE_EN
114
104
  }
115
105
  const operationId = await buildResponseCacheKey({
116
106
  documentString: getDocumentString(ctx.args),
107
+ variableValues: ctx.args.variableValues,
108
+ operationName: ctx.args.operationName,
117
109
  sessionId: session(ctx.args.contextValue),
118
- ...ctx.args,
119
110
  });
120
111
  if (((_a = enabled === null || enabled === void 0 ? void 0 : enabled(ctx.args.contextValue)) !== null && _a !== void 0 ? _a : true) === true) {
121
112
  const cachedResponse = await cache.get(operationId);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@envelop/response-cache",
3
- "version": "3.0.0-alpha-7c8ceda.0",
3
+ "version": "3.0.0-alpha-cadd145.0",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
6
  "@envelop/core": "^2.4.0",
@@ -0,0 +1,4 @@
1
+ import { ExecutionArgs } from 'graphql';
2
+ import { Plugin } from '@envelop/core';
3
+ export declare function useCacheDocumentString(): Plugin;
4
+ export declare function defaultGetDocumentString(executionArgs: ExecutionArgs): string;
@@ -7,9 +7,13 @@ import type { Cache } from './cache.js';
7
7
  export declare type BuildResponseCacheKeyFunction = (params: {
8
8
  /** Raw document string as sent from the client. */
9
9
  documentString: string;
10
+ /** Variable values as sent form the client. */
11
+ variableValues: ExecutionArgs['variableValues'];
12
+ /** The name of the GraphQL operation that should be executed from within the document. */
13
+ operationName?: Maybe<string>;
10
14
  /** optional sessionId for make unique cache keys based on the session. */
11
15
  sessionId?: Maybe<string>;
12
- } & ExecutionArgs) => Promise<string>;
16
+ }) => Promise<string>;
13
17
  export declare type GetDocumentStringFunction = (executionArgs: ExecutionArgs) => string;
14
18
  export declare type ShouldCacheResultFunction = (params: {
15
19
  result: ExecutionResult;
@@ -101,5 +105,4 @@ export declare const defaultBuildResponseCacheKey: BuildResponseCacheKeyFunction
101
105
  * By default, results with errors (unexpected, EnvelopError, or GraphQLError) are not cached.
102
106
  */
103
107
  export declare const defaultShouldCacheResult: ShouldCacheResultFunction;
104
- export declare function defaultGetDocumentString(executionArgs: ExecutionArgs): string;
105
108
  export declare function useResponseCache({ cache, ttl: globalTtl, session, enabled, ignoredTypes, ttlPerType, ttlPerSchemaCoordinate, idFields, invalidateViaMutation, buildResponseCacheKey, getDocumentString, shouldCacheResult, includeExtensionMetadata, }?: UseResponseCacheParameter): Plugin;