Skip to content

Commit b23257b

Browse files
feat(query): Add default mutation key (#1730)
* Add default mutation key * Fix tests * Fix tests * Fix formatting * Fix tests build
1 parent 1fb9c4a commit b23257b

File tree

9 files changed

+97
-13
lines changed

9 files changed

+97
-13
lines changed

packages/query/src/client.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ export const getQueryErrorType = (
341341
export const getHooksOptionImplementation = (
342342
isRequestOptions: boolean,
343343
httpClient: OutputHttpClient,
344+
operationName: string,
344345
mutator?: GeneratorMutator,
345346
) => {
346347
const options =
@@ -349,13 +350,18 @@ export const getHooksOptionImplementation = (
349350
: ', fetch: fetchOptions';
350351

351352
return isRequestOptions
352-
? `const {mutation: mutationOptions${
353+
? `const mutationKey = ['${operationName}'];
354+
const {mutation: mutationOptions${
353355
!mutator
354356
? options
355357
: mutator?.hasSecondArg
356358
? ', request: requestOptions'
357359
: ''
358-
}} = options ?? {};`
360+
}} = options ?
361+
options.mutation && 'mutationKey' in options.mutation && options.mutation.mutationKey ?
362+
options
363+
: {...options, mutation: {...options.mutation, mutationKey}}
364+
: {mutation: { mutationKey, }${mutator?.hasSecondArg ? ', request: undefined' : ''}${!mutator ? (httpClient === OutputHttpClient.AXIOS ? ', axios: undefined' : ', fetch: undefined') : ''}};`
359365
: '';
360366
};
361367

packages/query/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,7 @@ const generateQueryHook = async (
13251325
const hooksOptionImplementation = getHooksOptionImplementation(
13261326
isRequestOptions,
13271327
httpClient,
1328+
camel(operationName),
13281329
mutator,
13291330
);
13301331
const mutationOptionsFn = `export const ${mutationOptionsFnName} = <TError = ${errorType},

samples/react-query/basic/src/api/endpoints/petstoreFromFileSpecWithTransformer.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,14 @@ export const getCreatePetsMutationOptions = <
716716
{ data: CreatePetsBody; version?: number },
717717
TContext
718718
> => {
719-
const { mutation: mutationOptions } = options ?? {};
719+
const mutationKey = ['createPets'];
720+
const { mutation: mutationOptions } = options
721+
? options.mutation &&
722+
'mutationKey' in options.mutation &&
723+
options.mutation.mutationKey
724+
? options
725+
: { ...options, mutation: { ...options.mutation, mutationKey } }
726+
: { mutation: { mutationKey } };
720727

721728
const mutationFn: MutationFunction<
722729
Awaited<ReturnType<typeof createPets>>,

samples/react-query/custom-client/src/api/endpoints/petstoreFromFileSpecWithTransformer.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,14 @@ export const useCreatePetsMutationOptions = <
168168
{ data: BodyType<CreatePetsBody>; version?: number },
169169
TContext
170170
> => {
171-
const { mutation: mutationOptions } = options ?? {};
171+
const mutationKey = ['createPets'];
172+
const { mutation: mutationOptions } = options
173+
? options.mutation &&
174+
'mutationKey' in options.mutation &&
175+
options.mutation.mutationKey
176+
? options
177+
: { ...options, mutation: { ...options.mutation, mutationKey } }
178+
: { mutation: { mutationKey } };
172179

173180
const createPets = useCreatePetsHook();
174181

samples/react-query/custom-fetch/src/gen/pets/pets.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,14 @@ export const getCreatePetsMutationOptions = <
249249
{ data: CreatePetsBodyItem[] },
250250
TContext
251251
> => {
252-
const { mutation: mutationOptions, request: requestOptions } = options ?? {};
252+
const mutationKey = ['createPets'];
253+
const { mutation: mutationOptions, request: requestOptions } = options
254+
? options.mutation &&
255+
'mutationKey' in options.mutation &&
256+
options.mutation.mutationKey
257+
? options
258+
: { ...options, mutation: { ...options.mutation, mutationKey } }
259+
: { mutation: { mutationKey }, request: undefined };
253260

254261
const mutationFn: MutationFunction<
255262
Awaited<ReturnType<typeof createPets>>,
@@ -332,7 +339,14 @@ export const getUpdatePetsMutationOptions = <
332339
{ data: NonReadonly<Pet> },
333340
TContext
334341
> => {
335-
const { mutation: mutationOptions, request: requestOptions } = options ?? {};
342+
const mutationKey = ['updatePets'];
343+
const { mutation: mutationOptions, request: requestOptions } = options
344+
? options.mutation &&
345+
'mutationKey' in options.mutation &&
346+
options.mutation.mutationKey
347+
? options
348+
: { ...options, mutation: { ...options.mutation, mutationKey } }
349+
: { mutation: { mutationKey }, request: undefined };
336350

337351
const mutationFn: MutationFunction<
338352
Awaited<ReturnType<typeof updatePets>>,

samples/svelte-query/basic/src/api/endpoints/petstoreFromFileSpecWithTransformer.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,14 @@ export const getCreatePetsMutationOptions = <
149149
{ data: CreatePetsBody; version?: number },
150150
TContext
151151
> => {
152-
const { mutation: mutationOptions } = options ?? {};
152+
const mutationKey = ['createPets'];
153+
const { mutation: mutationOptions } = options
154+
? options.mutation &&
155+
'mutationKey' in options.mutation &&
156+
options.mutation.mutationKey
157+
? options
158+
: { ...options, mutation: { ...options.mutation, mutationKey } }
159+
: { mutation: { mutationKey } };
153160

154161
const mutationFn: MutationFunction<
155162
Awaited<ReturnType<typeof createPets>>,

samples/svelte-query/custom-fetch/src/gen/pets/pets.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,14 @@ export const getCreatePetsMutationOptions = <
196196
{ data: CreatePetsBodyItem[] },
197197
TContext
198198
> => {
199-
const { mutation: mutationOptions, request: requestOptions } = options ?? {};
199+
const mutationKey = ['createPets'];
200+
const { mutation: mutationOptions, request: requestOptions } = options
201+
? options.mutation &&
202+
'mutationKey' in options.mutation &&
203+
options.mutation.mutationKey
204+
? options
205+
: { ...options, mutation: { ...options.mutation, mutationKey } }
206+
: { mutation: { mutationKey }, request: undefined };
200207

201208
const mutationFn: MutationFunction<
202209
Awaited<ReturnType<typeof createPets>>,
@@ -279,7 +286,14 @@ export const getUpdatePetsMutationOptions = <
279286
{ data: NonReadonly<Pet> },
280287
TContext
281288
> => {
282-
const { mutation: mutationOptions, request: requestOptions } = options ?? {};
289+
const mutationKey = ['updatePets'];
290+
const { mutation: mutationOptions, request: requestOptions } = options
291+
? options.mutation &&
292+
'mutationKey' in options.mutation &&
293+
options.mutation.mutationKey
294+
? options
295+
: { ...options, mutation: { ...options.mutation, mutationKey } }
296+
: { mutation: { mutationKey }, request: undefined };
283297

284298
const mutationFn: MutationFunction<
285299
Awaited<ReturnType<typeof updatePets>>,

samples/vue-query/custom-fetch/src/gen/pets/pets.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,14 @@ export const getCreatePetsMutationOptions = <
199199
{ data: CreatePetsBodyItem[] },
200200
TContext
201201
> => {
202-
const { mutation: mutationOptions, request: requestOptions } = options ?? {};
202+
const mutationKey = ['createPets'];
203+
const { mutation: mutationOptions, request: requestOptions } = options
204+
? options.mutation &&
205+
'mutationKey' in options.mutation &&
206+
options.mutation.mutationKey
207+
? options
208+
: { ...options, mutation: { ...options.mutation, mutationKey } }
209+
: { mutation: { mutationKey }, request: undefined };
203210

204211
const mutationFn: MutationFunction<
205212
Awaited<ReturnType<typeof createPets>>,
@@ -282,7 +289,14 @@ export const getUpdatePetsMutationOptions = <
282289
{ data: NonReadonly<Pet> },
283290
TContext
284291
> => {
285-
const { mutation: mutationOptions, request: requestOptions } = options ?? {};
292+
const mutationKey = ['updatePets'];
293+
const { mutation: mutationOptions, request: requestOptions } = options
294+
? options.mutation &&
295+
'mutationKey' in options.mutation &&
296+
options.mutation.mutationKey
297+
? options
298+
: { ...options, mutation: { ...options.mutation, mutationKey } }
299+
: { mutation: { mutationKey }, request: undefined };
286300

287301
const mutationFn: MutationFunction<
288302
Awaited<ReturnType<typeof updatePets>>,

samples/vue-query/vue-query-basic/src/api/endpoints/petstoreFromFileSpecWithTransformer.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,14 @@ export const getCreatePetsMutationOptions = <
252252
{ data: CreatePetsBody; version?: number },
253253
TContext
254254
> => {
255-
const { mutation: mutationOptions } = options ?? {};
255+
const mutationKey = ['createPets'];
256+
const { mutation: mutationOptions } = options
257+
? options.mutation &&
258+
'mutationKey' in options.mutation &&
259+
options.mutation.mutationKey
260+
? options
261+
: { ...options, mutation: { ...options.mutation, mutationKey } }
262+
: { mutation: { mutationKey } };
256263

257264
const mutationFn: MutationFunction<
258265
Awaited<ReturnType<typeof createPets>>,
@@ -484,7 +491,14 @@ export const getPostApiV1UserLogoutMutationOptions = <
484491
void,
485492
TContext
486493
> => {
487-
const { mutation: mutationOptions } = options ?? {};
494+
const mutationKey = ['postApiV1UserLogout'];
495+
const { mutation: mutationOptions } = options
496+
? options.mutation &&
497+
'mutationKey' in options.mutation &&
498+
options.mutation.mutationKey
499+
? options
500+
: { ...options, mutation: { ...options.mutation, mutationKey } }
501+
: { mutation: { mutationKey } };
488502

489503
const mutationFn: MutationFunction<
490504
Awaited<ReturnType<typeof postApiV1UserLogout>>,

0 commit comments

Comments
 (0)