@envelop/response-cache 2.4.0-alpha-e9434aa.0 → 2.4.0-alpha-731c100.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/esm/cache.js +1 -2
- package/esm/hashSHA256.js +1 -5
- package/esm/in-memory-cache.js +5 -13
- package/esm/index.js +3 -19
- package/esm/plugin.js +19 -29
- package/package.json +2 -2
package/esm/cache.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
package/esm/hashSHA256.js
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.hashSHA256 = void 0;
|
|
4
1
|
const webCrypto = globalThis.crypto;
|
|
5
2
|
const sha256Browser = (bytes) => {
|
|
6
3
|
const hash = webCrypto.subtle.digest({ name: 'SHA-256' }, bytes);
|
|
@@ -30,7 +27,7 @@ if (typeof window === 'undefined' && !webCrypto) {
|
|
|
30
27
|
catch (_error) { }
|
|
31
28
|
}
|
|
32
29
|
}
|
|
33
|
-
const hashSHA256 = async (text) => {
|
|
30
|
+
export const hashSHA256 = async (text) => {
|
|
34
31
|
// Node.js support
|
|
35
32
|
if (nodeCrypto) {
|
|
36
33
|
return nodeCrypto.then(crypto => crypto.createHash('sha256').update(text).digest('hex'));
|
|
@@ -63,4 +60,3 @@ const hashSHA256 = async (text) => {
|
|
|
63
60
|
}
|
|
64
61
|
return Promise.resolve('');
|
|
65
62
|
};
|
|
66
|
-
exports.hashSHA256 = hashSHA256;
|
package/esm/in-memory-cache.js
CHANGED
|
@@ -1,14 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.defaultBuildEntityId = exports.createInMemoryCache = void 0;
|
|
7
|
-
const lru_cache_1 = __importDefault(require("lru-cache"));
|
|
8
|
-
const createInMemoryCache = (params) => {
|
|
1
|
+
import LRU from 'lru-cache';
|
|
2
|
+
export const createInMemoryCache = (params) => {
|
|
9
3
|
var _a;
|
|
10
|
-
const buildEntityId = (_a = params === null || params === void 0 ? void 0 : params.buildEntityId) !== null && _a !== void 0 ? _a :
|
|
11
|
-
const cachedResponses = new
|
|
4
|
+
const buildEntityId = (_a = params === null || params === void 0 ? void 0 : params.buildEntityId) !== null && _a !== void 0 ? _a : defaultBuildEntityId;
|
|
5
|
+
const cachedResponses = new LRU({
|
|
12
6
|
max: params === null || params === void 0 ? void 0 : params.max,
|
|
13
7
|
stale: false,
|
|
14
8
|
noDisposeOnSet: true,
|
|
@@ -83,6 +77,4 @@ const createInMemoryCache = (params) => {
|
|
|
83
77
|
},
|
|
84
78
|
};
|
|
85
79
|
};
|
|
86
|
-
|
|
87
|
-
const defaultBuildEntityId = (typename, id) => `${typename}:${id}`;
|
|
88
|
-
exports.defaultBuildEntityId = defaultBuildEntityId;
|
|
80
|
+
export const defaultBuildEntityId = (typename, id) => `${typename}:${id}`;
|
package/esm/index.js
CHANGED
|
@@ -1,19 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./in-memory-cache.js"), exports);
|
|
18
|
-
__exportStar(require("./plugin.js"), exports);
|
|
19
|
-
__exportStar(require("./cache.js"), exports);
|
|
1
|
+
export * from './in-memory-cache.js';
|
|
2
|
+
export * from './plugin.js';
|
|
3
|
+
export * from './cache.js';
|
package/esm/plugin.js
CHANGED
|
@@ -1,31 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const core_1 = require("@envelop/core");
|
|
8
|
-
const utils_1 = require("@graphql-tools/utils");
|
|
9
|
-
const graphql_1 = require("graphql");
|
|
10
|
-
const fast_json_stable_stringify_1 = __importDefault(require("fast-json-stable-stringify"));
|
|
11
|
-
const in_memory_cache_js_1 = require("./in-memory-cache.js");
|
|
12
|
-
const hashSHA256_js_1 = require("./hashSHA256.js");
|
|
1
|
+
import { isAsyncIterable } from '@envelop/core';
|
|
2
|
+
import { MapperKind, mapSchema } from '@graphql-tools/utils';
|
|
3
|
+
import { visit, TypeInfo, visitWithTypeInfo, defaultFieldResolver, } from 'graphql';
|
|
4
|
+
import jsonStableStringify from 'fast-json-stable-stringify';
|
|
5
|
+
import { createInMemoryCache } from './in-memory-cache.js';
|
|
6
|
+
import { hashSHA256 } from './hashSHA256.js';
|
|
13
7
|
const contextSymbol = Symbol('responseCache');
|
|
14
8
|
const rawDocumentStringSymbol = Symbol('rawDocumentString');
|
|
15
9
|
/**
|
|
16
10
|
* Default function used for building the response cache key.
|
|
17
11
|
* It is exported here for advanced use-cases. E.g. if you want to short circuit and serve responses from the cache on a global level in order to completely by-pass the GraphQL flow.
|
|
18
12
|
*/
|
|
19
|
-
const defaultBuildResponseCacheKey = params => {
|
|
13
|
+
export const defaultBuildResponseCacheKey = params => {
|
|
20
14
|
var _a, _b, _c;
|
|
21
|
-
return
|
|
15
|
+
return hashSHA256([
|
|
22
16
|
params.documentString,
|
|
23
17
|
(_a = params.operationName) !== null && _a !== void 0 ? _a : '',
|
|
24
|
-
(
|
|
18
|
+
jsonStableStringify((_b = params.variableValues) !== null && _b !== void 0 ? _b : {}),
|
|
25
19
|
(_c = params.sessionId) !== null && _c !== void 0 ? _c : '',
|
|
26
20
|
].join('|'));
|
|
27
21
|
};
|
|
28
|
-
exports.defaultBuildResponseCacheKey = defaultBuildResponseCacheKey;
|
|
29
22
|
/**
|
|
30
23
|
* Default function used to check if the result should be cached.
|
|
31
24
|
*
|
|
@@ -34,7 +27,7 @@ exports.defaultBuildResponseCacheKey = defaultBuildResponseCacheKey;
|
|
|
34
27
|
*
|
|
35
28
|
* By default, results with errors (unexpected, EnvelopError, or GraphQLError) are not cached.
|
|
36
29
|
*/
|
|
37
|
-
const defaultShouldCacheResult = (params) => {
|
|
30
|
+
export const defaultShouldCacheResult = (params) => {
|
|
38
31
|
if (params.result.errors) {
|
|
39
32
|
// eslint-disable-next-line no-console
|
|
40
33
|
console.warn('[useResponseCache] Failed to cache due to errors');
|
|
@@ -42,10 +35,8 @@ const defaultShouldCacheResult = (params) => {
|
|
|
42
35
|
}
|
|
43
36
|
return true;
|
|
44
37
|
};
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
exports.defaultGetDocumentStringFromContext = defaultGetDocumentStringFromContext;
|
|
48
|
-
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, getDocumentStringFromContext = exports.defaultGetDocumentStringFromContext, shouldCacheResult = exports.defaultShouldCacheResult,
|
|
38
|
+
export const defaultGetDocumentStringFromContext = context => context[rawDocumentStringSymbol];
|
|
39
|
+
export function useResponseCache({ cache = createInMemoryCache(), ttl: globalTtl = Infinity, session = () => null, enabled, ignoredTypes = [], ttlPerType = {}, ttlPerSchemaCoordinate = {}, idFields = ['id'], invalidateViaMutation = true, buildResponseCacheKey = defaultBuildResponseCacheKey, getDocumentStringFromContext = defaultGetDocumentStringFromContext, shouldCacheResult = defaultShouldCacheResult,
|
|
49
40
|
// eslint-disable-next-line dot-notation
|
|
50
41
|
includeExtensionMetadata = typeof process !== 'undefined' ? process.env['NODE_ENV'] === 'development' : false, } = {}) {
|
|
51
42
|
const appliedTransform = Symbol('responseCache.appliedTransform');
|
|
@@ -98,7 +89,7 @@ includeExtensionMetadata = typeof process !== 'undefined' ? process.env['NODE_EN
|
|
|
98
89
|
}
|
|
99
90
|
return {
|
|
100
91
|
onExecuteDone({ result, setResult }) {
|
|
101
|
-
if (
|
|
92
|
+
if (isAsyncIterable(result)) {
|
|
102
93
|
// eslint-disable-next-line no-console
|
|
103
94
|
console.warn('[useResponseCache] AsyncIterable returned from execute is currently unsupported.');
|
|
104
95
|
return;
|
|
@@ -146,8 +137,8 @@ includeExtensionMetadata = typeof process !== 'undefined' ? process.env['NODE_EN
|
|
|
146
137
|
}
|
|
147
138
|
}
|
|
148
139
|
if (ttlPerSchemaCoordinate) {
|
|
149
|
-
const typeInfo = new
|
|
150
|
-
|
|
140
|
+
const typeInfo = new TypeInfo(ctx.args.schema);
|
|
141
|
+
visit(ctx.args.document, visitWithTypeInfo(typeInfo, {
|
|
151
142
|
Field(fieldNode) {
|
|
152
143
|
const parentType = typeInfo.getParentType();
|
|
153
144
|
if (parentType) {
|
|
@@ -163,7 +154,7 @@ includeExtensionMetadata = typeof process !== 'undefined' ? process.env['NODE_EN
|
|
|
163
154
|
return {
|
|
164
155
|
onExecuteDone({ result, setResult }) {
|
|
165
156
|
var _a;
|
|
166
|
-
if (
|
|
157
|
+
if (isAsyncIterable(result)) {
|
|
167
158
|
// eslint-disable-next-line no-console
|
|
168
159
|
console.warn('[useResponseCache] AsyncIterable returned from execute is currently unsupported.');
|
|
169
160
|
return;
|
|
@@ -213,7 +204,6 @@ includeExtensionMetadata = typeof process !== 'undefined' ? process.env['NODE_EN
|
|
|
213
204
|
},
|
|
214
205
|
};
|
|
215
206
|
}
|
|
216
|
-
exports.useResponseCache = useResponseCache;
|
|
217
207
|
function isOperationDefinition(node) {
|
|
218
208
|
return (node === null || node === void 0 ? void 0 : node.kind) === 'OperationDefinition';
|
|
219
209
|
}
|
|
@@ -235,14 +225,14 @@ function runWith(input, callback) {
|
|
|
235
225
|
}
|
|
236
226
|
}
|
|
237
227
|
function applyResponseCacheLogic(schema, idFieldNames) {
|
|
238
|
-
return
|
|
239
|
-
[
|
|
228
|
+
return mapSchema(schema, {
|
|
229
|
+
[MapperKind.OBJECT_FIELD](fieldConfig, fieldName, typename) {
|
|
240
230
|
if (idFieldNames.includes(fieldName)) {
|
|
241
231
|
return {
|
|
242
232
|
...fieldConfig,
|
|
243
233
|
resolve(src, args, context, info) {
|
|
244
234
|
var _a;
|
|
245
|
-
const result = ((_a = fieldConfig.resolve) !== null && _a !== void 0 ? _a :
|
|
235
|
+
const result = ((_a = fieldConfig.resolve) !== null && _a !== void 0 ? _a : defaultFieldResolver)(src, args, context, info);
|
|
246
236
|
runWith(result, (id) => {
|
|
247
237
|
const ctx = context[contextSymbol];
|
|
248
238
|
if (ctx !== undefined) {
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@envelop/response-cache",
|
|
3
|
-
"version": "2.4.0-alpha-
|
|
3
|
+
"version": "2.4.0-alpha-731c100.0",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"peerDependencies": {
|
|
6
|
-
"@envelop/core": "2.4.0-alpha-
|
|
6
|
+
"@envelop/core": "2.4.0-alpha-731c100.0",
|
|
7
7
|
"graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|