Skip to content

Commit 8f32b50

Browse files
committed
feat(queryKey): add 'tags' option to query key configurations for better cache invalidation
1 parent 936d47b commit 8f32b50

File tree

11 files changed

+162
-9
lines changed

11 files changed

+162
-9
lines changed

packages/openapi-ts/src/plugins/@tanstack/angular-query-experimental/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const defaultConfig: TanStackAngularQueryPlugin['Config'] = {
2020
case: plugin.config.case ?? 'camelCase',
2121
enabled: true,
2222
name: '{{name}}InfiniteQueryKey',
23+
tags: false,
2324
},
2425
mappers: {
2526
boolean: (enabled) => ({ enabled }),
@@ -62,6 +63,7 @@ export const defaultConfig: TanStackAngularQueryPlugin['Config'] = {
6263
case: plugin.config.case ?? 'camelCase',
6364
enabled: true,
6465
name: '{{name}}QueryKey',
66+
tags: false,
6567
},
6668
mappers: {
6769
boolean: (enabled) => ({ enabled }),

packages/openapi-ts/src/plugins/@tanstack/angular-query-experimental/types.d.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ export type UserConfig = Plugin.Name<'@tanstack/angular-query-experimental'> & {
5656
* @see https://tanstack.com/query/v5/docs/framework/angular/reference/infiniteQueryOptions
5757
*/
5858
name?: StringName;
59+
/**
60+
* Whether to include operation tags in infinite query keys.
61+
* This will make query keys larger but provides better cache invalidation capabilities.
62+
*
63+
* @default false
64+
*/
65+
tags?: boolean;
5966
};
6067
/**
6168
* Configuration for generated infinite query options helpers.
@@ -173,6 +180,13 @@ export type UserConfig = Plugin.Name<'@tanstack/angular-query-experimental'> & {
173180
* @see https://tanstack.com/query/v5/docs/framework/angular/reference/queryKey
174181
*/
175182
name?: StringName;
183+
/**
184+
* Whether to include operation tags in query keys.
185+
* This will make query keys larger but provides better cache invalidation capabilities.
186+
*
187+
* @default false
188+
*/
189+
tags?: boolean;
176190
};
177191
/**
178192
* Configuration for generated query options helpers.
@@ -258,6 +272,13 @@ export type Config = Plugin.Name<'@tanstack/angular-query-experimental'> & {
258272
* @see https://tanstack.com/query/v5/docs/framework/angular/reference/infiniteQueryOptions
259273
*/
260274
name: StringName;
275+
/**
276+
* Whether to include operation tags in infinite query keys.
277+
* This will make query keys larger but provides better cache invalidation capabilities.
278+
*
279+
* @default false
280+
*/
281+
tags: boolean;
261282
};
262283
/**
263284
* Resolved configuration for generated infinite query options helpers.
@@ -345,6 +366,13 @@ export type Config = Plugin.Name<'@tanstack/angular-query-experimental'> & {
345366
* @see https://tanstack.com/query/v5/docs/framework/angular/reference/queryKey
346367
*/
347368
name: StringName;
369+
/**
370+
* Whether to include operation tags in query keys.
371+
* This will make query keys larger but provides better cache invalidation capabilities.
372+
*
373+
* @default false
374+
*/
375+
tags: boolean;
348376
};
349377
/**
350378
* Resolved configuration for generated query options helpers.

packages/openapi-ts/src/plugins/@tanstack/query-core/queryKey.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { Expression } from 'typescript';
2+
13
import { clientApi } from '../../../generate/client';
24
import { hasOperationDataRequired } from '../../../ir/operation';
35
import type { IR } from '../../../ir/types';
@@ -255,22 +257,23 @@ const createQueryKeyLiteral = ({
255257
namespace: 'value',
256258
});
257259

258-
const tagsExpression =
259-
operation.tags && operation.tags.length > 0
260-
? tsc.arrayLiteralExpression({
261-
elements: operation.tags.map((tag) =>
262-
tsc.stringLiteral({ text: tag }),
263-
),
264-
})
265-
: undefined;
260+
const config = isInfinite
261+
? plugin.config.infiniteQueryKeys
262+
: plugin.config.queryKeys;
263+
let tagsExpression: Expression | undefined;
264+
if (config.tags && operation.tags && operation.tags.length > 0) {
265+
tagsExpression = tsc.arrayLiteralExpression({
266+
elements: operation.tags.map((tag) => tsc.stringLiteral({ text: tag })),
267+
});
268+
}
266269

267270
const createQueryKeyCallExpression = tsc.callExpression({
268271
functionName: identifierCreateQueryKey.name || '',
269272
parameters: [
270273
tsc.ots.string(id),
271274
'options',
272275
tsc.ots.boolean(!!isInfinite),
273-
tagsExpression,
276+
tagsExpression ? tagsExpression : undefined,
274277
],
275278
});
276279
return createQueryKeyCallExpression;

packages/openapi-ts/src/plugins/@tanstack/react-query/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const defaultConfig: TanStackReactQueryPlugin['Config'] = {
2020
case: plugin.config.case ?? 'camelCase',
2121
enabled: true,
2222
name: '{{name}}InfiniteQueryKey',
23+
tags: false,
2324
},
2425
mappers: {
2526
boolean: (enabled) => ({ enabled }),
@@ -62,6 +63,7 @@ export const defaultConfig: TanStackReactQueryPlugin['Config'] = {
6263
case: plugin.config.case ?? 'camelCase',
6364
enabled: true,
6465
name: '{{name}}QueryKey',
66+
tags: false,
6567
},
6668
mappers: {
6769
boolean: (enabled) => ({ enabled }),

packages/openapi-ts/src/plugins/@tanstack/react-query/types.d.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ export type UserConfig = Plugin.Name<'@tanstack/react-query'> & {
6161
* @see https://tanstack.com/query/v5/docs/framework/react/reference/infiniteQueryOptions
6262
*/
6363
name?: StringName;
64+
/**
65+
* Whether to include operation tags in infinite query keys.
66+
* This will make query keys larger but provides better cache invalidation capabilities.
67+
*
68+
* @default false
69+
*/
70+
tags?: boolean;
6471
};
6572
/**
6673
* Configuration for generated infinite query options helpers.
@@ -178,6 +185,13 @@ export type UserConfig = Plugin.Name<'@tanstack/react-query'> & {
178185
* @see https://tanstack.com/query/v5/docs/framework/react/reference/queryKey
179186
*/
180187
name?: StringName;
188+
/**
189+
* Whether to include operation tags in query keys.
190+
* This will make query keys larger but provides better cache invalidation capabilities.
191+
*
192+
* @default false
193+
*/
194+
tags?: boolean;
181195
};
182196
/**
183197
* Configuration for generated query options helpers.
@@ -262,6 +276,13 @@ export type Config = Plugin.Name<'@tanstack/react-query'> & {
262276
* @see https://tanstack.com/query/v5/docs/framework/react/reference/infiniteQueryOptions
263277
*/
264278
name: StringName;
279+
/**
280+
* Whether to include operation tags in infinite query keys.
281+
* This will make query keys larger but provides better cache invalidation capabilities.
282+
*
283+
* @default false
284+
*/
285+
tags: boolean;
265286
};
266287
/**
267288
* Resolved configuration for generated infinite query options helpers.
@@ -346,6 +367,13 @@ export type Config = Plugin.Name<'@tanstack/react-query'> & {
346367
* @see https://tanstack.com/query/v5/docs/framework/react/reference/queryKey
347368
*/
348369
name: StringName;
370+
/**
371+
* Whether to include operation tags in query keys.
372+
* This will make query keys larger but provides better cache invalidation capabilities.
373+
*
374+
* @default false
375+
*/
376+
tags: boolean;
349377
};
350378
/**
351379
* Resolved configuration for generated query options helpers.

packages/openapi-ts/src/plugins/@tanstack/solid-query/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const defaultConfig: TanStackSolidQueryPlugin['Config'] = {
2020
case: plugin.config.case ?? 'camelCase',
2121
enabled: true,
2222
name: '{{name}}InfiniteQueryKey',
23+
tags: false,
2324
},
2425
mappers: {
2526
boolean: (enabled) => ({ enabled }),
@@ -62,6 +63,7 @@ export const defaultConfig: TanStackSolidQueryPlugin['Config'] = {
6263
case: plugin.config.case ?? 'camelCase',
6364
enabled: true,
6465
name: '{{name}}QueryKey',
66+
tags: false,
6567
},
6668
mappers: {
6769
boolean: (enabled) => ({ enabled }),

packages/openapi-ts/src/plugins/@tanstack/solid-query/types.d.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ export type UserConfig = Plugin.Name<'@tanstack/solid-query'> & {
5656
* @see https://tanstack.com/query/v5/docs/framework/solid/reference/createInfiniteQuery
5757
*/
5858
name?: StringName;
59+
/**
60+
* Whether to include operation tags in infinite query keys.
61+
* This will make query keys larger but provides better cache invalidation capabilities.
62+
*
63+
* @default false
64+
*/
65+
tags?: boolean;
5966
};
6067
/**
6168
* Configuration for generated infinite query options helpers.
@@ -173,6 +180,13 @@ export type UserConfig = Plugin.Name<'@tanstack/solid-query'> & {
173180
* @see https://tanstack.com/query/v5/docs/framework/solid/reference/queryKey
174181
*/
175182
name?: StringName;
183+
/**
184+
* Whether to include operation tags in query keys.
185+
* This will make query keys larger but provides better cache invalidation capabilities.
186+
*
187+
* @default false
188+
*/
189+
tags?: boolean;
176190
};
177191
/**
178192
* Configuration for generated query options helpers.
@@ -258,6 +272,13 @@ export type Config = Plugin.Name<'@tanstack/solid-query'> & {
258272
* @see https://tanstack.com/query/v5/docs/framework/solid/reference/createInfiniteQuery
259273
*/
260274
name: StringName;
275+
/**
276+
* Whether to include operation tags in infinite query keys.
277+
* This will make query keys larger but provides better cache invalidation capabilities.
278+
*
279+
* @default false
280+
*/
281+
tags: boolean;
261282
};
262283
/**
263284
* Resolved configuration for generated infinite query options helpers.
@@ -345,6 +366,13 @@ export type Config = Plugin.Name<'@tanstack/solid-query'> & {
345366
* @see https://tanstack.com/query/v5/docs/framework/solid/reference/queryKey
346367
*/
347368
name: StringName;
369+
/**
370+
* Whether to include operation tags in query keys.
371+
* This will make query keys larger but provides better cache invalidation capabilities.
372+
*
373+
* @default false
374+
*/
375+
tags: boolean;
348376
};
349377
/**
350378
* Resolved configuration for generated query options helpers.

packages/openapi-ts/src/plugins/@tanstack/svelte-query/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const defaultConfig: TanStackSvelteQueryPlugin['Config'] = {
2020
case: plugin.config.case ?? 'camelCase',
2121
enabled: true,
2222
name: '{{name}}InfiniteQueryKey',
23+
tags: false,
2324
},
2425
mappers: {
2526
boolean: (enabled) => ({ enabled }),
@@ -62,6 +63,7 @@ export const defaultConfig: TanStackSvelteQueryPlugin['Config'] = {
6263
case: plugin.config.case ?? 'camelCase',
6364
enabled: true,
6465
name: '{{name}}QueryKey',
66+
tags: false,
6567
},
6668
mappers: {
6769
boolean: (enabled) => ({ enabled }),

packages/openapi-ts/src/plugins/@tanstack/svelte-query/types.d.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ export type UserConfig = Plugin.Name<'@tanstack/svelte-query'> & {
5656
* @see https://tanstack.com/query/v5/docs/framework/svelte/reference/createInfiniteQuery
5757
*/
5858
name?: StringName;
59+
/**
60+
* Whether to include operation tags in infinite query keys.
61+
* This will make query keys larger but provides better cache invalidation capabilities.
62+
*
63+
* @default false
64+
*/
65+
tags?: boolean;
5966
};
6067
/**
6168
* Configuration for generated infinite query options helpers.
@@ -173,6 +180,13 @@ export type UserConfig = Plugin.Name<'@tanstack/svelte-query'> & {
173180
* @see https://tanstack.com/query/v5/docs/framework/svelte/reference/queryKey
174181
*/
175182
name?: StringName;
183+
/**
184+
* Whether to include operation tags in query keys.
185+
* This will make query keys larger but provides better cache invalidation capabilities.
186+
*
187+
* @default false
188+
*/
189+
tags?: boolean;
176190
};
177191
/**
178192
* Configuration for generated query options helpers.
@@ -258,6 +272,13 @@ export type Config = Plugin.Name<'@tanstack/svelte-query'> & {
258272
* @see https://tanstack.com/query/v5/docs/framework/svelte/reference/createInfiniteQuery
259273
*/
260274
name: StringName;
275+
/**
276+
* Whether to include operation tags in infinite query keys.
277+
* This will make query keys larger but provides better cache invalidation capabilities.
278+
*
279+
* @default false
280+
*/
281+
tags: boolean;
261282
};
262283
/**
263284
* Resolved configuration for generated infinite query options helpers.
@@ -345,6 +366,13 @@ export type Config = Plugin.Name<'@tanstack/svelte-query'> & {
345366
* @see https://tanstack.com/query/v5/docs/framework/svelte/reference/queryKey
346367
*/
347368
name: StringName;
369+
/**
370+
* Whether to include operation tags in query keys.
371+
* This will make query keys larger but provides better cache invalidation capabilities.
372+
*
373+
* @default false
374+
*/
375+
tags: boolean;
348376
};
349377
/**
350378
* Resolved configuration for generated query options helpers.

packages/openapi-ts/src/plugins/@tanstack/vue-query/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const defaultConfig: TanStackVueQueryPlugin['Config'] = {
2020
case: plugin.config.case ?? 'camelCase',
2121
enabled: true,
2222
name: '{{name}}InfiniteQueryKey',
23+
tags: false,
2324
},
2425
mappers: {
2526
boolean: (enabled) => ({ enabled }),
@@ -62,6 +63,7 @@ export const defaultConfig: TanStackVueQueryPlugin['Config'] = {
6263
case: plugin.config.case ?? 'camelCase',
6364
enabled: true,
6465
name: '{{name}}QueryKey',
66+
tags: false,
6567
},
6668
mappers: {
6769
boolean: (enabled) => ({ enabled }),

0 commit comments

Comments
 (0)