Skip to content

Commit

Permalink
[response-cache] deprecate ttlPerType option in favor of `ttlPerSch…
Browse files Browse the repository at this point in the history
…emaCoordinate`
  • Loading branch information
EmrysMyrddin committed Jun 28, 2024
1 parent 1ffb4dc commit 95797da
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions packages/plugins/response-cache/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,12 @@ export type UseResponseCacheParameter<PluginContext extends Record<string, any>
*/
ttl?: number;
/**
* Overwrite the ttl for query operations whose execution result contains a specific object type.
* Useful if the occurrence of a object time in the execution result should reduce or increase the TTL of the query operation.
* The TTL per type is always favored over the global TTL.
* @deprecated Use `ttlPerSchemaCoordinate` instead.
*/
ttlPerType?: Record<string, number>;
/**
* Overwrite the ttl for query operations whose selection contains a specific schema coordinate (e.g. Query.users).
* Useful if the selection of a specific field should reduce the TTL of the query operation.
* Overwrite the ttl for query operations whose selection contains a specific schema coordinate (e.g. Query or Query.users).
* Useful if the selection of an a object type or a specific field should reduce or increase the TTL of the query operation.
*
* The default value is `{}` and it will be merged with a `{ 'Query.__schema': 0 }` object.
* In the unusual case where you actually want to cache introspection query operations,
Expand Down Expand Up @@ -290,7 +288,7 @@ export function useResponseCache<PluginContext extends Record<string, any> = {}>
session,
enabled,
ignoredTypes = [],
ttlPerType = {},
ttlPerType,
ttlPerSchemaCoordinate = {},
scopePerSchemaCoordinate = {},
idFields = ['id'],
Expand All @@ -310,6 +308,12 @@ export function useResponseCache<PluginContext extends Record<string, any> = {}>

// never cache Introspections
ttlPerSchemaCoordinate = { 'Query.__schema': 0, ...ttlPerSchemaCoordinate };
if (ttlPerType) {
for (const [typeName, ttl] of Object.entries(ttlPerType)) {
ttlPerSchemaCoordinate[typeName] = ttl;
}
}

const documentMetadataOptions = {
queries: { invalidateViaMutation, ttlPerSchemaCoordinate },
mutations: { invalidateViaMutation }, // remove ttlPerSchemaCoordinate for mutations to skip TTL calculation
Expand Down Expand Up @@ -347,7 +351,7 @@ export function useResponseCache<PluginContext extends Record<string, any> = {}>
) as unknown as CacheControlDirective[] | undefined;
cacheControlAnnotations?.forEach(cacheControl => {
if (cacheControl.maxAge != null) {
ttlPerType[type.name] = cacheControl.maxAge * 1000;
ttlPerSchemaCoordinate[type.name] = cacheControl.maxAge * 1000;
}
if (cacheControl.scope) {
scopePerSchemaCoordinate[type.name] = cacheControl.scope;
Expand Down Expand Up @@ -450,8 +454,8 @@ export function useResponseCache<PluginContext extends Record<string, any> = {}>
}

types.add(typename);
if (typename in ttlPerType) {
const maybeTtl = ttlPerType[typename] as unknown;
if (typename in ttlPerSchemaCoordinate) {
const maybeTtl = ttlPerSchemaCoordinate[typename] as unknown;
currentTtl = calculateTtl(maybeTtl, currentTtl);
}
if (entityId != null) {
Expand All @@ -462,8 +466,8 @@ export function useResponseCache<PluginContext extends Record<string, any> = {}>
if (fieldData == null || (Array.isArray(fieldData) && fieldData.length === 0)) {
const inferredTypes = typePerSchemaCoordinateMap.get(`${typename}.${fieldName}`);
inferredTypes?.forEach(inferredType => {
if (inferredType in ttlPerType) {
const maybeTtl = ttlPerType[inferredType] as unknown;
if (inferredType in ttlPerSchemaCoordinate) {
const maybeTtl = ttlPerSchemaCoordinate[inferredType] as unknown;
currentTtl = calculateTtl(maybeTtl, currentTtl);
}
identifier.set(inferredType, { typename: inferredType });
Expand Down

0 comments on commit 95797da

Please sign in to comment.