@envelop/response-cache 3.0.1 → 3.0.2-alpha-1eb54ef.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/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@envelop/response-cache",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.2-alpha-1eb54ef.0",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"peerDependencies": {
|
|
6
|
-
"@envelop/core": "
|
|
6
|
+
"@envelop/core": "2.4.2-alpha-1eb54ef.0",
|
|
7
7
|
"graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"exports": {
|
|
29
29
|
".": {
|
|
30
30
|
"require": {
|
|
31
|
-
"types": "./typings/index.d.
|
|
31
|
+
"types": "./typings/index.d.cts",
|
|
32
32
|
"default": "./cjs/index.js"
|
|
33
33
|
},
|
|
34
34
|
"import": {
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
},
|
|
43
43
|
"./*": {
|
|
44
44
|
"require": {
|
|
45
|
-
"types": "./typings/*.d.
|
|
45
|
+
"types": "./typings/*.d.cts",
|
|
46
46
|
"default": "./cjs/*.js"
|
|
47
47
|
},
|
|
48
48
|
"import": {
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Maybe, PromiseOrValue } from '@envelop/core';
|
|
2
|
+
import type { ExecutionResult } from 'graphql';
|
|
3
|
+
export declare type CacheEntityRecord = {
|
|
4
|
+
typename: string;
|
|
5
|
+
id?: number | string;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Interface for implementing a cache that will be used for `useResponseCache`.
|
|
9
|
+
*/
|
|
10
|
+
export declare type Cache = {
|
|
11
|
+
/** set a cache response */
|
|
12
|
+
set(
|
|
13
|
+
/** id/hash of the operation */
|
|
14
|
+
id: string,
|
|
15
|
+
/** the result that should be cached */
|
|
16
|
+
data: ExecutionResult,
|
|
17
|
+
/** array of entity records that were collected during execution */
|
|
18
|
+
entities: Iterable<CacheEntityRecord>,
|
|
19
|
+
/** how long the operation should be cached */
|
|
20
|
+
ttl: number): PromiseOrValue<void>;
|
|
21
|
+
/** get a cached response */
|
|
22
|
+
get(id: string): PromiseOrValue<Maybe<ExecutionResult>>;
|
|
23
|
+
/** invalidate operations via typename or id */
|
|
24
|
+
invalidate(entities: Iterable<CacheEntityRecord>): PromiseOrValue<void>;
|
|
25
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const hashSHA256: (text: string) => Promise<string>;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Cache } from './cache.cjs';
|
|
2
|
+
export declare type BuildEntityId = (typename: string, id: number | string) => string;
|
|
3
|
+
export declare type InMemoryCacheParameter = {
|
|
4
|
+
/**
|
|
5
|
+
* Maximum amount of items in the cache. Defaults to `Infinity`.
|
|
6
|
+
*/
|
|
7
|
+
max?: number;
|
|
8
|
+
/**
|
|
9
|
+
* Customize how the cache entity id is built.
|
|
10
|
+
* By default the typename is concatenated with the id e.g. `User:1`
|
|
11
|
+
*/
|
|
12
|
+
buildEntityId?: BuildEntityId;
|
|
13
|
+
};
|
|
14
|
+
export declare const createInMemoryCache: (params?: InMemoryCacheParameter) => Cache;
|
|
15
|
+
export declare const defaultBuildEntityId: BuildEntityId;
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { Plugin, Maybe } from '@envelop/core';
|
|
2
|
+
import { ExecutionArgs, ExecutionResult } from 'graphql';
|
|
3
|
+
import type { Cache } from './cache.cjs';
|
|
4
|
+
/**
|
|
5
|
+
* Function for building the response cache key based on the input parameters
|
|
6
|
+
*/
|
|
7
|
+
export declare type BuildResponseCacheKeyFunction = (params: {
|
|
8
|
+
/** Raw document string as sent from the client. */
|
|
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>;
|
|
14
|
+
/** optional sessionId for make unique cache keys based on the session. */
|
|
15
|
+
sessionId: Maybe<string>;
|
|
16
|
+
}) => Promise<string>;
|
|
17
|
+
export declare type GetDocumentStringFunction = (executionArgs: ExecutionArgs) => string;
|
|
18
|
+
export declare type ShouldCacheResultFunction = (params: {
|
|
19
|
+
result: ExecutionResult;
|
|
20
|
+
}) => Boolean;
|
|
21
|
+
export declare type UseResponseCacheParameter<C = any> = {
|
|
22
|
+
cache?: Cache;
|
|
23
|
+
/**
|
|
24
|
+
* Maximum age in ms. Defaults to `Infinity`. Set it to 0 for disabling the global TTL.
|
|
25
|
+
*/
|
|
26
|
+
ttl?: number;
|
|
27
|
+
/**
|
|
28
|
+
* Overwrite the ttl for query operations whose execution result contains a specific object type.
|
|
29
|
+
* Useful if the occurrence of a object time in the execution result should reduce or increase the TTL of the query operation.
|
|
30
|
+
* The TTL per type is always favored over the global TTL.
|
|
31
|
+
*/
|
|
32
|
+
ttlPerType?: Record<string, number>;
|
|
33
|
+
/**
|
|
34
|
+
* Overwrite the ttl for query operations whose selection contains a specific schema coordinate (e.g. Query.users).
|
|
35
|
+
* Useful if the selection of a specific field should reduce the TTL of the query operation.
|
|
36
|
+
*
|
|
37
|
+
* The default value is `{}` and it will be merged with a `{ 'Query.__schema': 0 }` object.
|
|
38
|
+
* In the unusual case where you actually want to cache introspection query operations,
|
|
39
|
+
* you need to provide the value `{ 'Query.__schema': undefined }`.
|
|
40
|
+
*/
|
|
41
|
+
ttlPerSchemaCoordinate?: Record<string, number | undefined>;
|
|
42
|
+
/**
|
|
43
|
+
* Allows to cache responses based on the resolved session id.
|
|
44
|
+
* Return a unique value for each session.
|
|
45
|
+
* Return `null` or `undefined` to mark the session as public/global.
|
|
46
|
+
* Creates a global session by default.
|
|
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
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
session(context: C): string | undefined | null;
|
|
64
|
+
/**
|
|
65
|
+
* Specify whether the cache should be used based on the context.
|
|
66
|
+
* By default any request uses the cache.
|
|
67
|
+
*/
|
|
68
|
+
enabled?(context: C): boolean;
|
|
69
|
+
/**
|
|
70
|
+
* Skip caching of following the types.
|
|
71
|
+
*/
|
|
72
|
+
ignoredTypes?: string[];
|
|
73
|
+
/**
|
|
74
|
+
* List of fields that are used to identify a entity.
|
|
75
|
+
* Defaults to `["id"]`
|
|
76
|
+
*/
|
|
77
|
+
idFields?: Array<string>;
|
|
78
|
+
/**
|
|
79
|
+
* Whether the mutation execution result should be used for invalidating resources.
|
|
80
|
+
* Defaults to `true`
|
|
81
|
+
*/
|
|
82
|
+
invalidateViaMutation?: boolean;
|
|
83
|
+
/**
|
|
84
|
+
* Customize the behavior how the response cache key is computed from the document, variable values and sessionId.
|
|
85
|
+
* Defaults to `defaultBuildResponseCacheKey`
|
|
86
|
+
*/
|
|
87
|
+
buildResponseCacheKey?: BuildResponseCacheKeyFunction;
|
|
88
|
+
/**
|
|
89
|
+
* Function used for reading the document string that is used for building the response cache key from the execution arguments.
|
|
90
|
+
* By default, the useResponseCache plugin hooks into onParse and caches the original operation string in a WeakMap.
|
|
91
|
+
* If you are hard overriding parse you need to set this function, otherwise responses will not be cached or served from the cache.
|
|
92
|
+
* Defaults to `defaultGetDocumentString`
|
|
93
|
+
*
|
|
94
|
+
*/
|
|
95
|
+
getDocumentString?: GetDocumentStringFunction;
|
|
96
|
+
/**
|
|
97
|
+
* Include extension values that provide useful information, such as whether the cache was hit or which resources a mutation invalidated.
|
|
98
|
+
* Defaults to `true` if `process.env["NODE_ENV"]` is set to `"development"`, otherwise `false`.
|
|
99
|
+
*/
|
|
100
|
+
includeExtensionMetadata?: boolean;
|
|
101
|
+
/**
|
|
102
|
+
* Checks if the execution result should be cached or ignored. By default, any execution that
|
|
103
|
+
* raises any error, unexpected ot EnvelopError or GraphQLError are ignored.
|
|
104
|
+
* Use this function to customize the behavior, such as caching results that have an EnvelopError.
|
|
105
|
+
*/
|
|
106
|
+
shouldCacheResult?: ShouldCacheResultFunction;
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* Default function used for building the response cache key.
|
|
110
|
+
* 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.
|
|
111
|
+
*/
|
|
112
|
+
export declare const defaultBuildResponseCacheKey: BuildResponseCacheKeyFunction;
|
|
113
|
+
/**
|
|
114
|
+
* Default function used to check if the result should be cached.
|
|
115
|
+
*
|
|
116
|
+
* It is exported here for advanced use-cases. E.g. if you want to choose if
|
|
117
|
+
* results with certain error types should be cached.
|
|
118
|
+
*
|
|
119
|
+
* By default, results with errors (unexpected, EnvelopError, or GraphQLError) are not cached.
|
|
120
|
+
*/
|
|
121
|
+
export declare const defaultShouldCacheResult: ShouldCacheResultFunction;
|
|
122
|
+
export declare function useResponseCache({ cache, ttl: globalTtl, session, enabled, ignoredTypes, ttlPerType, ttlPerSchemaCoordinate, idFields, invalidateViaMutation, buildResponseCacheKey, getDocumentString, shouldCacheResult, includeExtensionMetadata, }: UseResponseCacheParameter): Plugin;
|