Skip to content

Commit 47cc816

Browse files
committed
feat: option to generate query invalidations
1 parent 5d9de71 commit 47cc816

File tree

6 files changed

+67
-0
lines changed

6 files changed

+67
-0
lines changed

docs/src/pages/reference/configuration/output.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,25 @@ export const prefetchGetCategories = async <
12091209
};
12101210
```
12111211

1212+
#### useInvalidate
1213+
1214+
Type: `Boolean`.
1215+
1216+
Use to generate <a href="https://tanstack.com/query/latest/docs/framework/react/guides/query-invalidation" target="_blank">invalidation</a> functions.
1217+
1218+
Example generated function:
1219+
1220+
```js
1221+
export const invalidateShowPetById = async (
1222+
queryClient: QueryClient, petId: string, options?: InvalidateOptions
1223+
): Promise<QueryClient> => {
1224+
1225+
await queryClient.invalidateQueries({ queryKey: getShowPetByIdQueryKey(petId) }, options);
1226+
1227+
return queryClient;
1228+
}
1229+
```
1230+
12121231
#### useInfiniteQueryParam
12131232

12141233
Type: `String`.

packages/core/src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,7 @@ export type NormalizedQueryOptions = {
578578
useSuspenseInfiniteQuery?: boolean;
579579
useInfiniteQueryParam?: string;
580580
usePrefetch?: boolean;
581+
useInvalidate?: boolean;
581582
options?: any;
582583
queryKey?: NormalizedMutator;
583584
queryOptions?: NormalizedMutator;
@@ -599,6 +600,7 @@ export type QueryOptions = {
599600
useSuspenseInfiniteQuery?: boolean;
600601
useInfiniteQueryParam?: string;
601602
usePrefetch?: boolean;
603+
useInvalidate?: boolean;
602604
options?: any;
603605
queryKey?: Mutator;
604606
queryOptions?: Mutator;

packages/orval/src/utils/options.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,9 @@ const normalizeQueryOptions = (
648648
...(isUndefined(queryOptions.usePrefetch)
649649
? {}
650650
: { usePrefetch: queryOptions.usePrefetch }),
651+
...(isUndefined(queryOptions.useInvalidate)
652+
? {}
653+
: { useInvalidate: queryOptions.useInvalidate }),
651654
...(isUndefined(queryOptions.useQuery)
652655
? {}
653656
: { useQuery: queryOptions.useQuery }),

packages/query/src/index.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ const SVELTE_QUERY_DEPENDENCIES_V3: GeneratorDependency[] = [
9696
{ name: 'UseInfiniteQueryStoreResult' },
9797
{ name: 'QueryKey' },
9898
{ name: 'CreateMutationResult' },
99+
{ name: 'InvalidateOptions' },
99100
],
100101
dependency: '@sveltestack/svelte-query',
101102
},
@@ -120,6 +121,7 @@ const SVELTE_QUERY_DEPENDENCIES: GeneratorDependency[] = [
120121
{ name: 'CreateMutationResult' },
121122
{ name: 'DataTag' },
122123
{ name: 'QueryClient' },
124+
{ name: 'InvalidateOptions' },
123125
],
124126
dependency: '@tanstack/svelte-query',
125127
},
@@ -177,6 +179,7 @@ const REACT_QUERY_DEPENDENCIES_V3: GeneratorDependency[] = [
177179
{ name: 'QueryKey' },
178180
{ name: 'QueryClient' },
179181
{ name: 'UseMutationResult' },
182+
{ name: 'InvalidateOptions' },
180183
],
181184
dependency: 'react-query',
182185
},
@@ -209,6 +212,7 @@ const REACT_QUERY_DEPENDENCIES: GeneratorDependency[] = [
209212
{ name: 'InfiniteData' },
210213
{ name: 'UseMutationResult' },
211214
{ name: 'DataTag' },
215+
{ name: 'InvalidateOptions' },
212216
],
213217
dependency: '@tanstack/react-query',
214218
},
@@ -268,6 +272,7 @@ const VUE_QUERY_DEPENDENCIES_V3: GeneratorDependency[] = [
268272
{ name: 'UseInfiniteQueryResult' },
269273
{ name: 'QueryKey' },
270274
{ name: 'UseMutationReturnType' },
275+
{ name: 'InvalidateOptions' },
271276
],
272277
dependency: 'vue-query/types',
273278
},
@@ -302,6 +307,7 @@ const VUE_QUERY_DEPENDENCIES: GeneratorDependency[] = [
302307
{ name: 'UseMutationReturnType' },
303308
{ name: 'DataTag' },
304309
{ name: 'QueryClient' },
310+
{ name: 'InvalidateOptions' },
305311
],
306312
dependency: '@tanstack/vue-query',
307313
},
@@ -772,6 +778,7 @@ const generateQueryImplementation = ({
772778
usePrefetch,
773779
useQuery,
774780
useInfinite,
781+
useInvalidate,
775782
}: {
776783
queryOption: {
777784
name: string;
@@ -806,6 +813,7 @@ const generateQueryImplementation = ({
806813
usePrefetch?: boolean;
807814
useQuery?: boolean;
808815
useInfinite?: boolean;
816+
useInvalidate?: boolean;
809817
}) => {
810818
const queryPropDefinitions = toObjectString(props, 'definition');
811819
const definedInitialDataQueryPropsDefinitions = toObjectString(
@@ -1086,6 +1094,14 @@ export function ${queryHookName}<TData = ${TData}, TError = ${errorType}>(\n ${q
10861094
const prefetchVarName = camel(`prefetch-${operationName}-${prefetchType}`);
10871095
const prefetchFnName = camel(`prefetch-${prefetchType}`);
10881096

1097+
const shouldGenerateInvalidate =
1098+
useInvalidate &&
1099+
(type === QueryType.QUERY ||
1100+
type === QueryType.INFINITE ||
1101+
(type === QueryType.SUSPENSE_QUERY && !useQuery) ||
1102+
(type === QueryType.SUSPENSE_INFINITE && !useInfinite));
1103+
const invalidateFnName = camel(`invalidate-${operationName}`);
1104+
10891105
return `
10901106
${queryOptionsFn}
10911107
@@ -1132,6 +1148,16 @@ ${
11321148
}\n`
11331149
: ''
11341150
}
1151+
${
1152+
shouldGenerateInvalidate
1153+
? `${doc}export const ${invalidateFnName} = async (\n queryClient: QueryClient, ${queryProps} options?: InvalidateOptions\n ): Promise<QueryClient> => {
1154+
1155+
await queryClient.invalidateQueries({ queryKey: ${queryKeyFnName}(${queryKeyProperties}) }, options);
1156+
1157+
return queryClient;
1158+
}\n`
1159+
: ''
1160+
}
11351161
`;
11361162
};
11371163

@@ -1433,6 +1459,7 @@ ${override.query.shouldExportQueryKey ? 'export ' : ''}const ${queryOption.query
14331459
usePrefetch: query.usePrefetch,
14341460
useQuery: query.useQuery,
14351461
useInfinite: query.useInfinite,
1462+
useInvalidate: query.useInvalidate,
14361463
})
14371464
);
14381465
}, '')}

packages/query/src/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export const normalizeQueryOptions = (
2222
): NormalizedQueryOptions => {
2323
return {
2424
...(queryOptions.usePrefetch ? { usePrefetch: true } : {}),
25+
...(queryOptions.useInvalidate ? { useInvalidate: true } : {}),
2526
...(queryOptions.useQuery ? { useQuery: true } : {}),
2627
...(queryOptions.useInfinite ? { useInfinite: true } : {}),
2728
...(queryOptions.useInfiniteQueryParam

tests/configs/react-query.config.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,4 +513,19 @@ export default defineConfig({
513513
target: '../specifications/models-with-special-char.yaml',
514514
},
515515
},
516+
useInvalidate: {
517+
output: {
518+
target: '../generated/react-query/use-invalidate/endpoints.ts',
519+
schemas: '../generated/react-query/use-invalidate/model',
520+
client: 'react-query',
521+
override: {
522+
query: {
523+
useInvalidate: true,
524+
},
525+
},
526+
},
527+
input: {
528+
target: '../specifications/petstore.yaml',
529+
},
530+
},
516531
});

0 commit comments

Comments
 (0)