@envelop/response-cache 6.2.0 → 6.3.0-alpha-20240710083620-c72a7363

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/plugin.js CHANGED
@@ -97,7 +97,7 @@ const getDocumentWithMetadataAndTTL = (0, utils_1.memoize4)(function addTypeName
97
97
  };
98
98
  return [(0, graphql_1.visit)(document, (0, graphql_1.visitWithTypeInfo)(typeInfo, visitor)), ttl];
99
99
  });
100
- function useResponseCache({ cache = (0, in_memory_cache_js_1.createInMemoryCache)(), ttl: globalTtl = Infinity, session, enabled, ignoredTypes = [], ttlPerType = {}, ttlPerSchemaCoordinate = {}, scopePerSchemaCoordinate = {}, idFields = ['id'], invalidateViaMutation = true, buildResponseCacheKey = exports.defaultBuildResponseCacheKey, getDocumentString = defaultGetDocumentString, shouldCacheResult = exports.defaultShouldCacheResult, includeExtensionMetadata = typeof process !== 'undefined'
100
+ function useResponseCache({ cache = (0, in_memory_cache_js_1.createInMemoryCache)(), ttl: globalTtl = Infinity, session, enabled, ignoredTypes = [], ttlPerType, ttlPerSchemaCoordinate = {}, scopePerSchemaCoordinate = {}, idFields = ['id'], invalidateViaMutation = true, buildResponseCacheKey = exports.defaultBuildResponseCacheKey, getDocumentString = defaultGetDocumentString, shouldCacheResult = exports.defaultShouldCacheResult, includeExtensionMetadata = typeof process !== 'undefined'
101
101
  ? // eslint-disable-next-line dot-notation
102
102
  process.env['NODE_ENV'] === 'development' || !!process.env['DEBUG']
103
103
  : false, }) {
@@ -107,6 +107,11 @@ function useResponseCache({ cache = (0, in_memory_cache_js_1.createInMemoryCache
107
107
  enabled = enabled ? (0, utils_1.memoize1)(enabled) : enabled;
108
108
  // never cache Introspections
109
109
  ttlPerSchemaCoordinate = { 'Query.__schema': 0, ...ttlPerSchemaCoordinate };
110
+ if (ttlPerType) {
111
+ for (const [typeName, ttl] of Object.entries(ttlPerType)) {
112
+ ttlPerSchemaCoordinate[typeName] = ttl;
113
+ }
114
+ }
110
115
  const documentMetadataOptions = {
111
116
  queries: { invalidateViaMutation, ttlPerSchemaCoordinate },
112
117
  mutations: { invalidateViaMutation }, // remove ttlPerSchemaCoordinate for mutations to skip TTL calculation
@@ -132,7 +137,7 @@ function useResponseCache({ cache = (0, in_memory_cache_js_1.createInMemoryCache
132
137
  const cacheControlAnnotations = (0, utils_1.getDirective)(schema, type, 'cacheControl');
133
138
  cacheControlAnnotations?.forEach(cacheControl => {
134
139
  if (cacheControl.maxAge != null) {
135
- ttlPerType[type.name] = cacheControl.maxAge * 1000;
140
+ ttlPerSchemaCoordinate[type.name] = cacheControl.maxAge * 1000;
136
141
  }
137
142
  if (cacheControl.scope) {
138
143
  scopePerSchemaCoordinate[type.name] = cacheControl.scope;
@@ -213,8 +218,8 @@ function useResponseCache({ cache = (0, in_memory_cache_js_1.createInMemoryCache
213
218
  return;
214
219
  }
215
220
  types.add(typename);
216
- if (typename in ttlPerType) {
217
- const maybeTtl = ttlPerType[typename];
221
+ if (typename in ttlPerSchemaCoordinate) {
222
+ const maybeTtl = ttlPerSchemaCoordinate[typename];
218
223
  currentTtl = calculateTtl(maybeTtl, currentTtl);
219
224
  }
220
225
  if (entityId != null) {
@@ -225,8 +230,8 @@ function useResponseCache({ cache = (0, in_memory_cache_js_1.createInMemoryCache
225
230
  if (fieldData == null || (Array.isArray(fieldData) && fieldData.length === 0)) {
226
231
  const inferredTypes = typePerSchemaCoordinateMap.get(`${typename}.${fieldName}`);
227
232
  inferredTypes?.forEach(inferredType => {
228
- if (inferredType in ttlPerType) {
229
- const maybeTtl = ttlPerType[inferredType];
233
+ if (inferredType in ttlPerSchemaCoordinate) {
234
+ const maybeTtl = ttlPerSchemaCoordinate[inferredType];
230
235
  currentTtl = calculateTtl(maybeTtl, currentTtl);
231
236
  }
232
237
  identifier.set(inferredType, { typename: inferredType });
package/esm/plugin.js CHANGED
@@ -90,7 +90,7 @@ const getDocumentWithMetadataAndTTL = memoize4(function addTypeNameToDocument(do
90
90
  };
91
91
  return [visit(document, visitWithTypeInfo(typeInfo, visitor)), ttl];
92
92
  });
93
- export function useResponseCache({ cache = createInMemoryCache(), ttl: globalTtl = Infinity, session, enabled, ignoredTypes = [], ttlPerType = {}, ttlPerSchemaCoordinate = {}, scopePerSchemaCoordinate = {}, idFields = ['id'], invalidateViaMutation = true, buildResponseCacheKey = defaultBuildResponseCacheKey, getDocumentString = defaultGetDocumentString, shouldCacheResult = defaultShouldCacheResult, includeExtensionMetadata = typeof process !== 'undefined'
93
+ export function useResponseCache({ cache = createInMemoryCache(), ttl: globalTtl = Infinity, session, enabled, ignoredTypes = [], ttlPerType, ttlPerSchemaCoordinate = {}, scopePerSchemaCoordinate = {}, idFields = ['id'], invalidateViaMutation = true, buildResponseCacheKey = defaultBuildResponseCacheKey, getDocumentString = defaultGetDocumentString, shouldCacheResult = defaultShouldCacheResult, includeExtensionMetadata = typeof process !== 'undefined'
94
94
  ? // eslint-disable-next-line dot-notation
95
95
  process.env['NODE_ENV'] === 'development' || !!process.env['DEBUG']
96
96
  : false, }) {
@@ -100,6 +100,11 @@ export function useResponseCache({ cache = createInMemoryCache(), ttl: globalTtl
100
100
  enabled = enabled ? memoize1(enabled) : enabled;
101
101
  // never cache Introspections
102
102
  ttlPerSchemaCoordinate = { 'Query.__schema': 0, ...ttlPerSchemaCoordinate };
103
+ if (ttlPerType) {
104
+ for (const [typeName, ttl] of Object.entries(ttlPerType)) {
105
+ ttlPerSchemaCoordinate[typeName] = ttl;
106
+ }
107
+ }
103
108
  const documentMetadataOptions = {
104
109
  queries: { invalidateViaMutation, ttlPerSchemaCoordinate },
105
110
  mutations: { invalidateViaMutation }, // remove ttlPerSchemaCoordinate for mutations to skip TTL calculation
@@ -125,7 +130,7 @@ export function useResponseCache({ cache = createInMemoryCache(), ttl: globalTtl
125
130
  const cacheControlAnnotations = getDirective(schema, type, 'cacheControl');
126
131
  cacheControlAnnotations?.forEach(cacheControl => {
127
132
  if (cacheControl.maxAge != null) {
128
- ttlPerType[type.name] = cacheControl.maxAge * 1000;
133
+ ttlPerSchemaCoordinate[type.name] = cacheControl.maxAge * 1000;
129
134
  }
130
135
  if (cacheControl.scope) {
131
136
  scopePerSchemaCoordinate[type.name] = cacheControl.scope;
@@ -206,8 +211,8 @@ export function useResponseCache({ cache = createInMemoryCache(), ttl: globalTtl
206
211
  return;
207
212
  }
208
213
  types.add(typename);
209
- if (typename in ttlPerType) {
210
- const maybeTtl = ttlPerType[typename];
214
+ if (typename in ttlPerSchemaCoordinate) {
215
+ const maybeTtl = ttlPerSchemaCoordinate[typename];
211
216
  currentTtl = calculateTtl(maybeTtl, currentTtl);
212
217
  }
213
218
  if (entityId != null) {
@@ -218,8 +223,8 @@ export function useResponseCache({ cache = createInMemoryCache(), ttl: globalTtl
218
223
  if (fieldData == null || (Array.isArray(fieldData) && fieldData.length === 0)) {
219
224
  const inferredTypes = typePerSchemaCoordinateMap.get(`${typename}.${fieldName}`);
220
225
  inferredTypes?.forEach(inferredType => {
221
- if (inferredType in ttlPerType) {
222
- const maybeTtl = ttlPerType[inferredType];
226
+ if (inferredType in ttlPerSchemaCoordinate) {
227
+ const maybeTtl = ttlPerSchemaCoordinate[inferredType];
223
228
  currentTtl = calculateTtl(maybeTtl, currentTtl);
224
229
  }
225
230
  identifier.set(inferredType, { typename: inferredType });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@envelop/response-cache",
3
- "version": "6.2.0",
3
+ "version": "6.3.0-alpha-20240710083620-c72a7363",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
6
  "@envelop/core": "^5.0.0",
@@ -28,14 +28,12 @@ export type UseResponseCacheParameter<PluginContext extends Record<string, any>
28
28
  */
29
29
  ttl?: number;
30
30
  /**
31
- * Overwrite the ttl for query operations whose execution result contains a specific object type.
32
- * Useful if the occurrence of a object time in the execution result should reduce or increase the TTL of the query operation.
33
- * The TTL per type is always favored over the global TTL.
31
+ * @deprecated Use `ttlPerSchemaCoordinate` instead.
34
32
  */
35
33
  ttlPerType?: Record<string, number>;
36
34
  /**
37
- * Overwrite the ttl for query operations whose selection contains a specific schema coordinate (e.g. Query.users).
38
- * Useful if the selection of a specific field should reduce the TTL of the query operation.
35
+ * Overwrite the ttl for query operations whose selection contains a specific schema coordinate (e.g. Query or Query.users).
36
+ * Useful if the selection of an a object type or a specific field should reduce or increase the TTL of the query operation.
39
37
  *
40
38
  * The default value is `{}` and it will be merged with a `{ 'Query.__schema': 0 }` object.
41
39
  * In the unusual case where you actually want to cache introspection query operations,
@@ -28,14 +28,12 @@ export type UseResponseCacheParameter<PluginContext extends Record<string, any>
28
28
  */
29
29
  ttl?: number;
30
30
  /**
31
- * Overwrite the ttl for query operations whose execution result contains a specific object type.
32
- * Useful if the occurrence of a object time in the execution result should reduce or increase the TTL of the query operation.
33
- * The TTL per type is always favored over the global TTL.
31
+ * @deprecated Use `ttlPerSchemaCoordinate` instead.
34
32
  */
35
33
  ttlPerType?: Record<string, number>;
36
34
  /**
37
- * Overwrite the ttl for query operations whose selection contains a specific schema coordinate (e.g. Query.users).
38
- * Useful if the selection of a specific field should reduce the TTL of the query operation.
35
+ * Overwrite the ttl for query operations whose selection contains a specific schema coordinate (e.g. Query or Query.users).
36
+ * Useful if the selection of an a object type or a specific field should reduce or increase the TTL of the query operation.
39
37
  *
40
38
  * The default value is `{}` and it will be merged with a `{ 'Query.__schema': 0 }` object.
41
39
  * In the unusual case where you actually want to cache introspection query operations,