@envelop/response-cache 3.0.0-alpha-e8a9318.0 → 3.0.0-alpha-0d610c4.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/README.md +39 -21
- package/cjs/plugin.js +2 -2
- package/esm/plugin.js +2 -2
- package/package.json +1 -1
- package/typings/plugin.d.ts +17 -3
package/README.md
CHANGED
|
@@ -44,7 +44,10 @@ import { useResponseCache } from '@envelop/response-cache';
|
|
|
44
44
|
const getEnveloped = envelop({
|
|
45
45
|
plugins: [
|
|
46
46
|
// ... other plugins ...
|
|
47
|
-
useResponseCache(
|
|
47
|
+
useResponseCache({
|
|
48
|
+
// use global cache for all operations
|
|
49
|
+
session: () => null,
|
|
50
|
+
}),
|
|
48
51
|
],
|
|
49
52
|
});
|
|
50
53
|
```
|
|
@@ -61,12 +64,31 @@ const getEnveloped = envelop({
|
|
|
61
64
|
plugins: [
|
|
62
65
|
// ... other plugins ...
|
|
63
66
|
useResponseCache({ cache }),
|
|
67
|
+
session: () => null, // use global cache for all operations
|
|
64
68
|
],
|
|
65
69
|
});
|
|
66
70
|
```
|
|
67
71
|
|
|
68
72
|
> Note: The in-memory LRU cache is not suitable for serverless deployments. Instead, consider the Redis cache provided by `@envelop/response-cache-redis`.
|
|
69
73
|
|
|
74
|
+
### Cache based on session/user
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
import { envelop } from '@envelop/core';
|
|
78
|
+
import { useResponseCache } from '@envelop/response-cache';
|
|
79
|
+
|
|
80
|
+
const getEnveloped = envelop({
|
|
81
|
+
plugins: [
|
|
82
|
+
// ... other plugins ...
|
|
83
|
+
useResponseCache({
|
|
84
|
+
ttl: 2000,
|
|
85
|
+
// context is the GraphQL context used for execution
|
|
86
|
+
session: context => String(context.user?.id),
|
|
87
|
+
}),
|
|
88
|
+
],
|
|
89
|
+
});
|
|
90
|
+
```
|
|
91
|
+
|
|
70
92
|
### Redis Cache
|
|
71
93
|
|
|
72
94
|
```bash
|
|
@@ -93,14 +115,17 @@ const redis = new Redis({
|
|
|
93
115
|
password: '1234567890',
|
|
94
116
|
});
|
|
95
117
|
|
|
96
|
-
const redis = new Redis(
|
|
118
|
+
const redis = new Redis('rediss://:1234567890@my-redis-db.example.com:30652');
|
|
97
119
|
|
|
98
120
|
const cache = createRedisCache({ redis });
|
|
99
121
|
|
|
100
122
|
const getEnveloped = envelop({
|
|
101
123
|
plugins: [
|
|
102
124
|
// ... other plugins ...
|
|
103
|
-
useResponseCache({
|
|
125
|
+
useResponseCache({
|
|
126
|
+
cache,
|
|
127
|
+
session: () => null, // use global cache for all operations
|
|
128
|
+
}),
|
|
104
129
|
],
|
|
105
130
|
});
|
|
106
131
|
```
|
|
@@ -120,6 +145,7 @@ const getEnveloped = envelop({
|
|
|
120
145
|
// ... other plugins ...
|
|
121
146
|
useResponseCache({
|
|
122
147
|
ttl: 2000, // cached execution results become stale after 2 seconds
|
|
148
|
+
session: () => null, // use global cache for all operations
|
|
123
149
|
}),
|
|
124
150
|
],
|
|
125
151
|
});
|
|
@@ -167,24 +193,6 @@ const getEnveloped = envelop({
|
|
|
167
193
|
});
|
|
168
194
|
```
|
|
169
195
|
|
|
170
|
-
### Cache based on session/user
|
|
171
|
-
|
|
172
|
-
```ts
|
|
173
|
-
import { envelop } from '@envelop/core';
|
|
174
|
-
import { useResponseCache } from '@envelop/response-cache';
|
|
175
|
-
|
|
176
|
-
const getEnveloped = envelop({
|
|
177
|
-
plugins: [
|
|
178
|
-
// ... other plugins ...
|
|
179
|
-
useResponseCache({
|
|
180
|
-
ttl: 2000,
|
|
181
|
-
// context is the GraphQL context used for execution
|
|
182
|
-
session: context => String(context.user?.id),
|
|
183
|
-
}),
|
|
184
|
-
],
|
|
185
|
-
});
|
|
186
|
-
```
|
|
187
|
-
|
|
188
196
|
### Disable cache based on session/user
|
|
189
197
|
|
|
190
198
|
```ts
|
|
@@ -198,6 +206,7 @@ const getEnveloped = envelop({
|
|
|
198
206
|
ttl: 2000,
|
|
199
207
|
// context is the GraphQL context used for execution
|
|
200
208
|
enabled: context => context.user?.role !== 'admin',
|
|
209
|
+
session: () => null,
|
|
201
210
|
}),
|
|
202
211
|
],
|
|
203
212
|
});
|
|
@@ -231,6 +240,7 @@ const getEnveloped = envelop({
|
|
|
231
240
|
// ... other plugins ...
|
|
232
241
|
useResponseCache({
|
|
233
242
|
shouldCacheResult = myCustomShouldCacheResult,
|
|
243
|
+
session: () => null,
|
|
234
244
|
}),
|
|
235
245
|
],
|
|
236
246
|
});
|
|
@@ -253,6 +263,7 @@ const getEnveloped = envelop({
|
|
|
253
263
|
ttlPerSchemaCoordinate: {
|
|
254
264
|
'Query.__schema': undefined, // cache infinitely
|
|
255
265
|
},
|
|
266
|
+
session: () => null,
|
|
256
267
|
}),
|
|
257
268
|
],
|
|
258
269
|
});
|
|
@@ -271,6 +282,7 @@ const getEnveloped = envelop({
|
|
|
271
282
|
ttlPerSchemaCoordinate: {
|
|
272
283
|
'Query.__schema': 10_000, // cache for 10 seconds
|
|
273
284
|
},
|
|
285
|
+
session: () => null,
|
|
274
286
|
}),
|
|
275
287
|
],
|
|
276
288
|
});
|
|
@@ -287,6 +299,7 @@ const getEnveloped = envelop({
|
|
|
287
299
|
// ... other plugins ...
|
|
288
300
|
useResponseCache({
|
|
289
301
|
ttl: 2000, // cached execution results become stale after 2 seconds
|
|
302
|
+
session: () => null,
|
|
290
303
|
}),
|
|
291
304
|
],
|
|
292
305
|
});
|
|
@@ -305,6 +318,7 @@ const getEnveloped = envelop({
|
|
|
305
318
|
ttl: 2000,
|
|
306
319
|
// use the `_id` instead of `id` field.
|
|
307
320
|
idFields: ['_id'],
|
|
321
|
+
session: () => null,
|
|
308
322
|
}),
|
|
309
323
|
],
|
|
310
324
|
});
|
|
@@ -323,6 +337,7 @@ const getEnveloped = envelop({
|
|
|
323
337
|
ttl: 2000,
|
|
324
338
|
// some might prefer invalidating based on a database write log
|
|
325
339
|
invalidateViaMutation: false,
|
|
340
|
+
session: () => null,
|
|
326
341
|
}),
|
|
327
342
|
],
|
|
328
343
|
});
|
|
@@ -345,6 +360,7 @@ const getEnveloped = envelop({
|
|
|
345
360
|
ttl: 2000,
|
|
346
361
|
// we pass the cache instance to the request.
|
|
347
362
|
cache,
|
|
363
|
+
session: () => null,
|
|
348
364
|
}),
|
|
349
365
|
],
|
|
350
366
|
});
|
|
@@ -379,6 +395,7 @@ const getEnveloped = envelop({
|
|
|
379
395
|
ttl: 2000,
|
|
380
396
|
// we pass the cache instance to the request.
|
|
381
397
|
cache,
|
|
398
|
+
session: () => null,
|
|
382
399
|
}),
|
|
383
400
|
],
|
|
384
401
|
});
|
|
@@ -395,6 +412,7 @@ const getEnveloped = envelop({
|
|
|
395
412
|
useResponseCache({
|
|
396
413
|
ttl: 2000,
|
|
397
414
|
includeExtensionMetadata: true,
|
|
415
|
+
session: () => null,
|
|
398
416
|
}),
|
|
399
417
|
],
|
|
400
418
|
});
|
package/cjs/plugin.js
CHANGED
|
@@ -41,9 +41,9 @@ const defaultShouldCacheResult = (params) => {
|
|
|
41
41
|
return true;
|
|
42
42
|
};
|
|
43
43
|
exports.defaultShouldCacheResult = defaultShouldCacheResult;
|
|
44
|
-
function useResponseCache({ cache = (0, in_memory_cache_js_1.createInMemoryCache)(), ttl: globalTtl = Infinity, session
|
|
44
|
+
function useResponseCache({ cache = (0, in_memory_cache_js_1.createInMemoryCache)(), ttl: globalTtl = Infinity, session, enabled, ignoredTypes = [], ttlPerType = {}, ttlPerSchemaCoordinate = {}, idFields = ['id'], invalidateViaMutation = true, buildResponseCacheKey = exports.defaultBuildResponseCacheKey, getDocumentString = cache_document_str_js_1.defaultGetDocumentString, shouldCacheResult = exports.defaultShouldCacheResult,
|
|
45
45
|
// eslint-disable-next-line dot-notation
|
|
46
|
-
includeExtensionMetadata = typeof process !== 'undefined' ? process.env['NODE_ENV'] === 'development' : false, }
|
|
46
|
+
includeExtensionMetadata = typeof process !== 'undefined' ? process.env['NODE_ENV'] === 'development' : false, }) {
|
|
47
47
|
const appliedTransform = Symbol('responseCache.appliedTransform');
|
|
48
48
|
const ignoredTypesMap = new Set(ignoredTypes);
|
|
49
49
|
// never cache Introspections
|
package/esm/plugin.js
CHANGED
|
@@ -35,9 +35,9 @@ export const defaultShouldCacheResult = (params) => {
|
|
|
35
35
|
}
|
|
36
36
|
return true;
|
|
37
37
|
};
|
|
38
|
-
export function useResponseCache({ cache = createInMemoryCache(), ttl: globalTtl = Infinity, session
|
|
38
|
+
export function useResponseCache({ cache = createInMemoryCache(), ttl: globalTtl = Infinity, session, enabled, ignoredTypes = [], ttlPerType = {}, ttlPerSchemaCoordinate = {}, idFields = ['id'], invalidateViaMutation = true, buildResponseCacheKey = defaultBuildResponseCacheKey, getDocumentString = defaultGetDocumentString, shouldCacheResult = defaultShouldCacheResult,
|
|
39
39
|
// eslint-disable-next-line dot-notation
|
|
40
|
-
includeExtensionMetadata = typeof process !== 'undefined' ? process.env['NODE_ENV'] === 'development' : false, }
|
|
40
|
+
includeExtensionMetadata = typeof process !== 'undefined' ? process.env['NODE_ENV'] === 'development' : false, }) {
|
|
41
41
|
const appliedTransform = Symbol('responseCache.appliedTransform');
|
|
42
42
|
const ignoredTypesMap = new Set(ignoredTypes);
|
|
43
43
|
// never cache Introspections
|
package/package.json
CHANGED
package/typings/plugin.d.ts
CHANGED
|
@@ -12,7 +12,7 @@ export declare type BuildResponseCacheKeyFunction = (params: {
|
|
|
12
12
|
/** The name of the GraphQL operation that should be executed from within the document. */
|
|
13
13
|
operationName?: Maybe<string>;
|
|
14
14
|
/** optional sessionId for make unique cache keys based on the session. */
|
|
15
|
-
sessionId
|
|
15
|
+
sessionId: Maybe<string>;
|
|
16
16
|
}) => Promise<string>;
|
|
17
17
|
export declare type GetDocumentStringFunction = (executionArgs: ExecutionArgs) => string;
|
|
18
18
|
export declare type ShouldCacheResultFunction = (params: {
|
|
@@ -45,8 +45,22 @@ export declare type UseResponseCacheParameter<C = any> = {
|
|
|
45
45
|
* Return `null` or `undefined` to mark the session as public/global.
|
|
46
46
|
* Creates a global session by default.
|
|
47
47
|
* @param context GraphQL Context
|
|
48
|
+
*
|
|
49
|
+
* **Global Example:**
|
|
50
|
+
* ```ts
|
|
51
|
+
* useResponseCache({
|
|
52
|
+
* session: () => null,
|
|
53
|
+
* });
|
|
54
|
+
* ```
|
|
55
|
+
*
|
|
56
|
+
* **User Specific with global fallback example:**
|
|
57
|
+
* ```ts
|
|
58
|
+
* useResponseCache({
|
|
59
|
+
* session: (context) => context.user?.id ?? null,
|
|
60
|
+
* });
|
|
61
|
+
* ```
|
|
48
62
|
*/
|
|
49
|
-
session
|
|
63
|
+
session(context: C): string | undefined | null;
|
|
50
64
|
/**
|
|
51
65
|
* Specify whether the cache should be used based on the context.
|
|
52
66
|
* By default any request uses the cache.
|
|
@@ -105,4 +119,4 @@ export declare const defaultBuildResponseCacheKey: BuildResponseCacheKeyFunction
|
|
|
105
119
|
* By default, results with errors (unexpected, EnvelopError, or GraphQLError) are not cached.
|
|
106
120
|
*/
|
|
107
121
|
export declare const defaultShouldCacheResult: ShouldCacheResultFunction;
|
|
108
|
-
export declare function useResponseCache({ cache, ttl: globalTtl, session, enabled, ignoredTypes, ttlPerType, ttlPerSchemaCoordinate, idFields, invalidateViaMutation, buildResponseCacheKey, getDocumentString, shouldCacheResult, includeExtensionMetadata, }
|
|
122
|
+
export declare function useResponseCache({ cache, ttl: globalTtl, session, enabled, ignoredTypes, ttlPerType, ttlPerSchemaCoordinate, idFields, invalidateViaMutation, buildResponseCacheKey, getDocumentString, shouldCacheResult, includeExtensionMetadata, }: UseResponseCacheParameter): Plugin;
|