diff --git a/.changeset/twenty-camels-wonder.md b/.changeset/twenty-camels-wonder.md new file mode 100644 index 0000000000..80b0baad4c --- /dev/null +++ b/.changeset/twenty-camels-wonder.md @@ -0,0 +1,25 @@ +--- +'@envelop/response-cache': minor +--- + +Deprecate `ttlPerType` in favor of `ttlPerSchemaCoordinate`, for a more streamlined API + +## Migration instructions + +If you where using `ttlPerType`, you can merge the object into the `ttlPerSchemaCoordinate`, the +syntax doesn't change. + +```diff +useResponseCache({ + session: null, +- ttlPerType: { +- User: 10_000, +- Profile: 600_000, +- }, + ttlPerSchemaCoordinate: { + 'Query.me': 0 ++ User: 10_000, ++ Profile: 600_000, + } +}) +``` diff --git a/packages/plugins/response-cache/src/plugin.ts b/packages/plugins/response-cache/src/plugin.ts index dc16179f0b..97bcb4fbdf 100644 --- a/packages/plugins/response-cache/src/plugin.ts +++ b/packages/plugins/response-cache/src/plugin.ts @@ -63,14 +63,12 @@ export type UseResponseCacheParameter */ 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; /** - * 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, @@ -290,7 +288,7 @@ export function useResponseCache = {}> session, enabled, ignoredTypes = [], - ttlPerType = {}, + ttlPerType, ttlPerSchemaCoordinate = {}, scopePerSchemaCoordinate = {}, idFields = ['id'], @@ -310,6 +308,12 @@ export function useResponseCache = {}> // 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 @@ -347,7 +351,7 @@ export function useResponseCache = {}> ) 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; @@ -450,8 +454,8 @@ export function useResponseCache = {}> } 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) { @@ -462,8 +466,8 @@ export function useResponseCache = {}> 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 }); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e11bd58b05..f16ac8b4b9 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -9550,8 +9550,8 @@ packages: resolution: {integrity: sha512-zWmv4RSuB9r2mYQw3zxQuHWeU+42aKi1wWig/j4ele4ygELZ7PEO6MM7rim9oAQH2A5MWfsAVf/jPvTPgCbvUQ==} engines: {node: '>=0.6'} - qs@6.12.2: - resolution: {integrity: sha512-x+NLUpx9SYrcwXtX7ob1gnkSems4i/mGZX5SlYxwIau6RrUSODO89TR/XDGGpn5RPWSYIB+aSfuSlV5+CmbTBg==} + qs@6.12.3: + resolution: {integrity: sha512-AWJm14H1vVaO/iNZ4/hO+HyaTehuy9nRqVdkTqlJt0HWvBiBIEXFmb4C0DGeYo3Xes9rrEW+TxHsaigCbN5ICQ==} engines: {node: '>=0.6'} qs@6.7.0: @@ -21882,7 +21882,7 @@ snapshots: dependencies: side-channel: 1.0.6 - qs@6.12.2: + qs@6.12.3: dependencies: side-channel: 1.0.6 @@ -23705,7 +23705,7 @@ snapshots: url@0.11.3: dependencies: punycode: 1.4.1 - qs: 6.12.2 + qs: 6.12.3 urlpattern-polyfill@10.0.0: {}