@envelop/response-cache 3.0.0-alpha-9dd2d31.0 → 3.0.0-alpha-e8a9318.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.
- package/cjs/cache-document-str.js +26 -0
- package/cjs/plugin.js +8 -19
- package/esm/cache-document-str.js +21 -0
- package/esm/plugin.js +7 -17
- package/package.json +1 -1
- package/typings/cache-document-str.d.ts +4 -0
- package/typings/plugin.d.ts +0 -1
|
@@ -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.
|
|
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
|
-
|
|
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();
|
|
@@ -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,
|
|
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();
|
package/package.json
CHANGED
package/typings/plugin.d.ts
CHANGED
|
@@ -105,5 +105,4 @@ export declare const defaultBuildResponseCacheKey: BuildResponseCacheKeyFunction
|
|
|
105
105
|
* By default, results with errors (unexpected, EnvelopError, or GraphQLError) are not cached.
|
|
106
106
|
*/
|
|
107
107
|
export declare const defaultShouldCacheResult: ShouldCacheResultFunction;
|
|
108
|
-
export declare function defaultGetDocumentString(executionArgs: ExecutionArgs): string;
|
|
109
108
|
export declare function useResponseCache({ cache, ttl: globalTtl, session, enabled, ignoredTypes, ttlPerType, ttlPerSchemaCoordinate, idFields, invalidateViaMutation, buildResponseCacheKey, getDocumentString, shouldCacheResult, includeExtensionMetadata, }?: UseResponseCacheParameter): Plugin;
|