@envelop/response-cache 2.3.4-alpha-fae0892.0 → 2.3.4-alpha-f55cd76.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/index.js +7 -9
- package/index.mjs +8 -10
- package/package.json +1 -1
- package/plugin.d.ts +3 -4
package/index.js
CHANGED
|
@@ -154,7 +154,6 @@ const hashSHA256 = async (text) => {
|
|
|
154
154
|
};
|
|
155
155
|
|
|
156
156
|
const contextSymbol = Symbol('responseCache');
|
|
157
|
-
const rawDocumentStringSymbol = Symbol('rawDocumentString');
|
|
158
157
|
/**
|
|
159
158
|
* Default function used for building the response cache key.
|
|
160
159
|
* 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.
|
|
@@ -184,18 +183,16 @@ const defaultShouldCacheResult = (params) => {
|
|
|
184
183
|
}
|
|
185
184
|
return true;
|
|
186
185
|
};
|
|
187
|
-
// @deprecated This will be removed in the next major
|
|
188
|
-
const defaultGetDocumentStringFromContext = context => context[rawDocumentStringSymbol];
|
|
189
186
|
const documentStringByDocument = new WeakMap();
|
|
190
|
-
function
|
|
191
|
-
let documentString = documentStringByDocument.get(
|
|
187
|
+
function defaultGetDocumentStringFromContext(_context, args) {
|
|
188
|
+
let documentString = documentStringByDocument.get(args.document);
|
|
192
189
|
if (!documentString) {
|
|
193
|
-
documentString = graphql.print(
|
|
194
|
-
documentStringByDocument.set(
|
|
190
|
+
documentString = graphql.print(args.document);
|
|
191
|
+
documentStringByDocument.set(args.document, documentString);
|
|
195
192
|
}
|
|
196
193
|
return documentString;
|
|
197
194
|
}
|
|
198
|
-
function useResponseCache({ cache = createInMemoryCache(), ttl: globalTtl = Infinity, session = () => null, enabled, ignoredTypes = [], ttlPerType = {}, ttlPerSchemaCoordinate = {}, idFields = ['id'], invalidateViaMutation = true, buildResponseCacheKey = defaultBuildResponseCacheKey, getDocumentStringFromContext, shouldCacheResult = defaultShouldCacheResult,
|
|
195
|
+
function useResponseCache({ cache = createInMemoryCache(), ttl: globalTtl = Infinity, session = () => null, enabled, ignoredTypes = [], ttlPerType = {}, ttlPerSchemaCoordinate = {}, idFields = ['id'], invalidateViaMutation = true, buildResponseCacheKey = defaultBuildResponseCacheKey, getDocumentStringFromContext = defaultGetDocumentStringFromContext, shouldCacheResult = defaultShouldCacheResult,
|
|
199
196
|
// eslint-disable-next-line dot-notation
|
|
200
197
|
includeExtensionMetadata = typeof process !== 'undefined' ? process.env['NODE_ENV'] === 'development' : false, } = {}) {
|
|
201
198
|
const appliedTransform = Symbol('responseCache.appliedTransform');
|
|
@@ -264,7 +261,8 @@ includeExtensionMetadata = typeof process !== 'undefined' ? process.env['NODE_EN
|
|
|
264
261
|
},
|
|
265
262
|
};
|
|
266
263
|
}
|
|
267
|
-
|
|
264
|
+
// Return a document string even if the user provided function doesn't return a valid result.
|
|
265
|
+
const documentString = (_a = getDocumentStringFromContext(ctx.args.contextValue, ctx.args)) !== null && _a !== void 0 ? _a : defaultGetDocumentStringFromContext(ctx.args.contextValue, ctx.args);
|
|
268
266
|
const operationId = await buildResponseCacheKey({
|
|
269
267
|
documentString,
|
|
270
268
|
variableValues: ctx.args.variableValues,
|
package/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import LRU from 'lru-cache';
|
|
2
2
|
import { isAsyncIterable } from '@envelop/core';
|
|
3
3
|
import { mapSchema, MapperKind } from '@graphql-tools/utils';
|
|
4
|
-
import { defaultFieldResolver, TypeInfo, visit, visitWithTypeInfo
|
|
4
|
+
import { print, defaultFieldResolver, TypeInfo, visit, visitWithTypeInfo } from 'graphql';
|
|
5
5
|
import jsonStableStringify from 'fast-json-stable-stringify';
|
|
6
6
|
|
|
7
7
|
const createInMemoryCache = (params) => {
|
|
@@ -148,7 +148,6 @@ const hashSHA256 = async (text) => {
|
|
|
148
148
|
};
|
|
149
149
|
|
|
150
150
|
const contextSymbol = Symbol('responseCache');
|
|
151
|
-
const rawDocumentStringSymbol = Symbol('rawDocumentString');
|
|
152
151
|
/**
|
|
153
152
|
* Default function used for building the response cache key.
|
|
154
153
|
* 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.
|
|
@@ -178,18 +177,16 @@ const defaultShouldCacheResult = (params) => {
|
|
|
178
177
|
}
|
|
179
178
|
return true;
|
|
180
179
|
};
|
|
181
|
-
// @deprecated This will be removed in the next major
|
|
182
|
-
const defaultGetDocumentStringFromContext = context => context[rawDocumentStringSymbol];
|
|
183
180
|
const documentStringByDocument = new WeakMap();
|
|
184
|
-
function
|
|
185
|
-
let documentString = documentStringByDocument.get(
|
|
181
|
+
function defaultGetDocumentStringFromContext(_context, args) {
|
|
182
|
+
let documentString = documentStringByDocument.get(args.document);
|
|
186
183
|
if (!documentString) {
|
|
187
|
-
documentString = print(
|
|
188
|
-
documentStringByDocument.set(
|
|
184
|
+
documentString = print(args.document);
|
|
185
|
+
documentStringByDocument.set(args.document, documentString);
|
|
189
186
|
}
|
|
190
187
|
return documentString;
|
|
191
188
|
}
|
|
192
|
-
function useResponseCache({ cache = createInMemoryCache(), ttl: globalTtl = Infinity, session = () => null, enabled, ignoredTypes = [], ttlPerType = {}, ttlPerSchemaCoordinate = {}, idFields = ['id'], invalidateViaMutation = true, buildResponseCacheKey = defaultBuildResponseCacheKey, getDocumentStringFromContext, shouldCacheResult = defaultShouldCacheResult,
|
|
189
|
+
function useResponseCache({ cache = createInMemoryCache(), ttl: globalTtl = Infinity, session = () => null, enabled, ignoredTypes = [], ttlPerType = {}, ttlPerSchemaCoordinate = {}, idFields = ['id'], invalidateViaMutation = true, buildResponseCacheKey = defaultBuildResponseCacheKey, getDocumentStringFromContext = defaultGetDocumentStringFromContext, shouldCacheResult = defaultShouldCacheResult,
|
|
193
190
|
// eslint-disable-next-line dot-notation
|
|
194
191
|
includeExtensionMetadata = typeof process !== 'undefined' ? process.env['NODE_ENV'] === 'development' : false, } = {}) {
|
|
195
192
|
const appliedTransform = Symbol('responseCache.appliedTransform');
|
|
@@ -258,7 +255,8 @@ includeExtensionMetadata = typeof process !== 'undefined' ? process.env['NODE_EN
|
|
|
258
255
|
},
|
|
259
256
|
};
|
|
260
257
|
}
|
|
261
|
-
|
|
258
|
+
// Return a document string even if the user provided function doesn't return a valid result.
|
|
259
|
+
const documentString = (_a = getDocumentStringFromContext(ctx.args.contextValue, ctx.args)) !== null && _a !== void 0 ? _a : defaultGetDocumentStringFromContext(ctx.args.contextValue, ctx.args);
|
|
262
260
|
const operationId = await buildResponseCacheKey({
|
|
263
261
|
documentString,
|
|
264
262
|
variableValues: ctx.args.variableValues,
|
package/package.json
CHANGED
package/plugin.d.ts
CHANGED
|
@@ -14,7 +14,7 @@ export declare type BuildResponseCacheKeyFunction = (params: {
|
|
|
14
14
|
/** optional sessionId for make unique cache keys based on the session. */
|
|
15
15
|
sessionId?: Maybe<string>;
|
|
16
16
|
}) => Promise<string>;
|
|
17
|
-
export declare type GetDocumentStringFromContextFunction = (
|
|
17
|
+
export declare type GetDocumentStringFromContextFunction = (context: DefaultContext, executionArgs: ExecutionArgs) => Maybe<string>;
|
|
18
18
|
export declare type ShouldCacheResultFunction = (params: {
|
|
19
19
|
result: ExecutionResult;
|
|
20
20
|
}) => Boolean;
|
|
@@ -73,11 +73,10 @@ export declare type UseResponseCacheParameter<C = any> = {
|
|
|
73
73
|
buildResponseCacheKey?: BuildResponseCacheKeyFunction;
|
|
74
74
|
/**
|
|
75
75
|
* Function used for reading the document string that is used for building the response cache key from the context object.
|
|
76
|
-
* By default, the useResponseCache plugin hooks into onParse and
|
|
76
|
+
* By default, the useResponseCache plugin hooks into onParse and caches the original operation string in a WeakMap.
|
|
77
77
|
* If you are hard overriding parse you need to set this function, otherwise responses will not be cached or served from the cache.
|
|
78
78
|
* Defaults to `defaultGetDocumentStringFromContext`
|
|
79
79
|
*
|
|
80
|
-
* @deprecated No longer being used in the next major version.
|
|
81
80
|
*/
|
|
82
81
|
getDocumentStringFromContext?: GetDocumentStringFromContextFunction;
|
|
83
82
|
/**
|
|
@@ -106,5 +105,5 @@ export declare const defaultBuildResponseCacheKey: BuildResponseCacheKeyFunction
|
|
|
106
105
|
* By default, results with errors (unexpected, EnvelopError, or GraphQLError) are not cached.
|
|
107
106
|
*/
|
|
108
107
|
export declare const defaultShouldCacheResult: ShouldCacheResultFunction;
|
|
109
|
-
export declare
|
|
108
|
+
export declare function defaultGetDocumentStringFromContext(_context: DefaultContext, args: ExecutionArgs): string;
|
|
110
109
|
export declare function useResponseCache({ cache, ttl: globalTtl, session, enabled, ignoredTypes, ttlPerType, ttlPerSchemaCoordinate, idFields, invalidateViaMutation, buildResponseCacheKey, getDocumentStringFromContext, shouldCacheResult, includeExtensionMetadata, }?: UseResponseCacheParameter): Plugin;
|