Skip to content

Commit 936d47b

Browse files
committed
feat(queryKey): add optional tags parameter to query key functions
1 parent ebf969d commit 936d47b

File tree

1 file changed

+43
-1
lines changed
  • packages/openapi-ts/src/plugins/@tanstack/query-core

1 file changed

+43
-1
lines changed

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

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ export const createQueryKeyFunction = ({
5959
name: 'infinite',
6060
type: tsc.typeReferenceNode({ typeName: 'boolean' }),
6161
},
62+
{
63+
isRequired: false,
64+
name: 'tags',
65+
type: tsc.typeReferenceNode({ typeName: 'ReadonlyArray<string>' }),
66+
},
6267
],
6368
returnType: tsc.typeTupleNode({
6469
types: [returnType],
@@ -100,6 +105,22 @@ export const createQueryKeyFunction = ({
100105
],
101106
}),
102107
}),
108+
tsc.ifStatement({
109+
expression: tsc.identifier({ text: 'tags' }),
110+
thenStatement: tsc.block({
111+
statements: [
112+
tsc.expressionToStatement({
113+
expression: tsc.binaryExpression({
114+
left: tsc.propertyAccessExpression({
115+
expression: 'params',
116+
name: 'tags',
117+
}),
118+
right: tsc.identifier({ text: 'tags' }),
119+
}),
120+
}),
121+
],
122+
}),
123+
}),
103124
tsc.ifStatement({
104125
expression: tsc.propertyAccessExpression({
105126
expression: optionsIdentifier,
@@ -218,10 +239,12 @@ export const createQueryKeyFunction = ({
218239
const createQueryKeyLiteral = ({
219240
id,
220241
isInfinite,
242+
operation,
221243
plugin,
222244
}: {
223245
id: string;
224246
isInfinite?: boolean;
247+
operation: IR.OperationObject;
225248
plugin: PluginInstance;
226249
}) => {
227250
const file = plugin.context.file({ id: plugin.name })!;
@@ -231,12 +254,23 @@ const createQueryKeyLiteral = ({
231254
case: plugin.config.case,
232255
namespace: 'value',
233256
});
257+
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;
266+
234267
const createQueryKeyCallExpression = tsc.callExpression({
235268
functionName: identifierCreateQueryKey.name || '',
236269
parameters: [
237270
tsc.ots.string(id),
238271
'options',
239-
isInfinite ? tsc.ots.boolean(true) : undefined,
272+
tsc.ots.boolean(!!isInfinite),
273+
tagsExpression,
240274
],
241275
});
242276
return createQueryKeyCallExpression;
@@ -259,6 +293,13 @@ export const createQueryKeyType = ({ plugin }: { plugin: PluginInstance }) => {
259293
keyword: 'boolean',
260294
}),
261295
},
296+
{
297+
isRequired: false,
298+
name: 'tags',
299+
type: tsc.typeReferenceNode({
300+
typeName: 'ReadonlyArray<string>',
301+
}),
302+
},
262303
];
263304

264305
const queryKeyType = tsc.typeAliasDeclaration({
@@ -337,6 +378,7 @@ export const queryKeyStatement = ({
337378
statements: createQueryKeyLiteral({
338379
id: operation.id,
339380
isInfinite,
381+
operation,
340382
plugin,
341383
}),
342384
}),

0 commit comments

Comments
 (0)