Skip to content

Commit 02a62ac

Browse files
committed
[response-cache] deprecate ttlPerType option in favor of ttlPerSchemaCoordinate
1 parent 20dd748 commit 02a62ac

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

packages/plugins/response-cache/src/plugin.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,12 @@ export type UseResponseCacheParameter<PluginContext extends Record<string, any>
6363
*/
6464
ttl?: number;
6565
/**
66-
* Overwrite the ttl for query operations whose execution result contains a specific object type.
67-
* Useful if the occurrence of a object time in the execution result should reduce or increase the TTL of the query operation.
68-
* The TTL per type is always favored over the global TTL.
66+
* @deprecated Use `ttlPerSchemaCoordinate` instead.
6967
*/
7068
ttlPerType?: Record<string, number>;
7169
/**
72-
* Overwrite the ttl for query operations whose selection contains a specific schema coordinate (e.g. Query.users).
73-
* Useful if the selection of a specific field should reduce the TTL of the query operation.
70+
* Overwrite the ttl for query operations whose selection contains a specific schema coordinate (e.g. Query or Query.users).
71+
* Useful if the selection of an a object type or a specific field should reduce or increase the TTL of the query operation.
7472
*
7573
* The default value is `{}` and it will be merged with a `{ 'Query.__schema': 0 }` object.
7674
* In the unusual case where you actually want to cache introspection query operations,
@@ -290,7 +288,7 @@ export function useResponseCache<PluginContext extends Record<string, any> = {}>
290288
session,
291289
enabled,
292290
ignoredTypes = [],
293-
ttlPerType = {},
291+
ttlPerType,
294292
ttlPerSchemaCoordinate = {},
295293
scopePerSchemaCoordinate = {},
296294
idFields = ['id'],
@@ -310,6 +308,12 @@ export function useResponseCache<PluginContext extends Record<string, any> = {}>
310308

311309
// never cache Introspections
312310
ttlPerSchemaCoordinate = { 'Query.__schema': 0, ...ttlPerSchemaCoordinate };
311+
if (ttlPerType) {
312+
for (const [typeName, ttl] of Object.entries(ttlPerType)) {
313+
ttlPerSchemaCoordinate[typeName] = ttl;
314+
}
315+
}
316+
313317
const documentMetadataOptions = {
314318
queries: { invalidateViaMutation, ttlPerSchemaCoordinate },
315319
mutations: { invalidateViaMutation }, // remove ttlPerSchemaCoordinate for mutations to skip TTL calculation
@@ -347,7 +351,7 @@ export function useResponseCache<PluginContext extends Record<string, any> = {}>
347351
) as unknown as CacheControlDirective[] | undefined;
348352
cacheControlAnnotations?.forEach(cacheControl => {
349353
if (cacheControl.maxAge != null) {
350-
ttlPerType[type.name] = cacheControl.maxAge * 1000;
354+
ttlPerSchemaCoordinate[type.name] = cacheControl.maxAge * 1000;
351355
}
352356
if (cacheControl.scope) {
353357
scopePerSchemaCoordinate[type.name] = cacheControl.scope;
@@ -450,8 +454,8 @@ export function useResponseCache<PluginContext extends Record<string, any> = {}>
450454
}
451455

452456
types.add(typename);
453-
if (typename in ttlPerType) {
454-
const maybeTtl = ttlPerType[typename] as unknown;
457+
if (typename in ttlPerSchemaCoordinate) {
458+
const maybeTtl = ttlPerSchemaCoordinate[typename] as unknown;
455459
currentTtl = calculateTtl(maybeTtl, currentTtl);
456460
}
457461
if (entityId != null) {
@@ -462,8 +466,8 @@ export function useResponseCache<PluginContext extends Record<string, any> = {}>
462466
if (fieldData == null || (Array.isArray(fieldData) && fieldData.length === 0)) {
463467
const inferredTypes = typePerSchemaCoordinateMap.get(`${typename}.${fieldName}`);
464468
inferredTypes?.forEach(inferredType => {
465-
if (inferredType in ttlPerType) {
466-
const maybeTtl = ttlPerType[inferredType] as unknown;
469+
if (inferredType in ttlPerSchemaCoordinate) {
470+
const maybeTtl = ttlPerSchemaCoordinate[inferredType] as unknown;
467471
currentTtl = calculateTtl(maybeTtl, currentTtl);
468472
}
469473
identifier.set(inferredType, { typename: inferredType });

0 commit comments

Comments
 (0)