Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[response-cache] deprecate ttlPerType option in favor of ttlPerSchemaCoordinate #2258

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .changeset/twenty-camels-wonder.md
Original file line number Diff line number Diff line change
@@ -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,
}
})
```
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
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading