Skip to content

Commit 329218a

Browse files
authored
fix(query): correctly check arg numbers, add operation info, add docs (#1974)
1 parent e7c70af commit 329218a

File tree

4 files changed

+62
-3
lines changed

4 files changed

+62
-3
lines changed

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

+20-1
Original file line numberDiff line numberDiff line change
@@ -1124,7 +1124,7 @@ module.exports = {
11241124
query: {
11251125
mutationOptions: {
11261126
path: './api/mutator/custom-mutator-options.ts',
1127-
name: 'customMutatorOptionsFn',
1127+
name: 'useCustomMutatorOptions',
11281128
// default: true
11291129
},
11301130
},
@@ -1134,6 +1134,25 @@ module.exports = {
11341134
};
11351135
```
11361136

1137+
```ts
1138+
// custom-mutator-options.ts
1139+
1140+
export const useCustomMutatorOptions = <T, TError, TData, TContext>(
1141+
options: UseMutationOptions<T, TError, TData, TContext> &
1142+
Required<
1143+
Pick<UseMutationOptions<T, TError, TData, TContext>, 'mutationFn'>
1144+
>,
1145+
/* Optional */ path: { url: string },
1146+
/* Optional */ operation: { operationId: string; operationName: string },
1147+
) => {
1148+
const queryClient = useQueryClient();
1149+
if (operation.operationId === 'createPet') {
1150+
queryClient.invalidateQueries({ queryKey: getGetPetsQueryKey() });
1151+
}
1152+
return options;
1153+
};
1154+
```
1155+
11371156
##### signal
11381157

11391158
Type: `Boolean`.

packages/query/src/index.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1385,8 +1385,12 @@ ${hooksOptionImplementation}
13851385
? `const customOptions = ${
13861386
mutationOptionsMutator.name
13871387
}({...mutationOptions, mutationFn}${
1388+
mutationOptionsMutator.hasSecondArg
1389+
? `, { url: \`${route.replaceAll('/${', '/{')}\` }`
1390+
: ''
1391+
}${
13881392
mutationOptionsMutator.hasThirdArg
1389-
? `, { url: \`${route}\` }`
1393+
? `, { operationId: '${operationId}', operationName: '${operationName}' }`
13901394
: ''
13911395
});`
13921396
: ''

tests/configs/react-query.config.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,27 @@ export default defineConfig({
5555
schemas: '../generated/react-query/split-query-key/model',
5656
client: 'react-query',
5757
override: {
58-
query: { shouldSplitQueryKey: true },
58+
query: {
59+
shouldSplitQueryKey: true,
60+
},
61+
},
62+
},
63+
input: {
64+
target: '../specifications/petstore.yaml',
65+
},
66+
},
67+
petstoreCustomMutatorOptions: {
68+
output: {
69+
target: '../generated/react-query/custom-mutator-options/endpoints.ts',
70+
schemas: '../generated/react-query/custom-mutator-options/model',
71+
client: 'react-query',
72+
override: {
73+
query: {
74+
mutationOptions: {
75+
path: '../mutators/custom-mutation.ts',
76+
name: 'useCustomMutation',
77+
},
78+
},
5979
},
6080
},
6181
input: {

tests/mutators/custom-mutation.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { UseMutationOptions, useQueryClient } from '@tanstack/react-query';
2+
3+
export const useCustomMutation = <T, TError, TData, TContext>(
4+
options: UseMutationOptions<T, TError, TData, TContext> &
5+
Required<
6+
Pick<UseMutationOptions<T, TError, TData, TContext>, 'mutationFn'>
7+
>,
8+
_: { url: string },
9+
operation: { operationId: string; operationName: string },
10+
) => {
11+
const queryClient = useQueryClient();
12+
if (operation.operationId === 'deletePetById') {
13+
queryClient.invalidateQueries({ queryKey: ['/pets'] });
14+
}
15+
return options;
16+
};

0 commit comments

Comments
 (0)