Skip to content

Commit

Permalink
fix(query): type return of useMutation query
Browse files Browse the repository at this point in the history
  • Loading branch information
david10sing committed Feb 11, 2024
1 parent 288f488 commit 01f8d22
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 29 deletions.
61 changes: 44 additions & 17 deletions packages/query/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ const REACT_QUERY_DEPENDENCIES_V3: GeneratorDependency[] = [
{ name: 'UseInfiniteQueryResult' },
{ name: 'QueryKey' },
{ name: 'QueryClient' },
{ name: 'UseMutationResult' },
],
dependency: 'react-query',
},
Expand All @@ -186,6 +187,7 @@ const REACT_QUERY_DEPENDENCIES: GeneratorDependency[] = [
{ name: 'QueryKey' },
{ name: 'QueryClient' },
{ name: 'InfiniteData' },
{ name: 'UseMutationResult' },
],
dependency: '@tanstack/react-query',
},
Expand Down Expand Up @@ -728,6 +730,27 @@ const generateQueryReturnType = ({
}
};

const generateMutatorReturnType = ({
outputClient,
dataType,
variableType,
}: {
outputClient: OutputClient | OutputClientFunc;
dataType: unknown;
variableType: unknown;
}) => {
if (outputClient !== OutputClient.REACT_QUERY) {
return '';
}

return `: UseMutationResult<
Awaited<ReturnType<${dataType}>>,
TError,
${variableType},
TContext
>`;
};

const getQueryOptions = ({
isRequestOptions,
mutator,
Expand Down Expand Up @@ -1373,17 +1396,17 @@ const generateQueryHook = async (

const mutationOptionsFn = `export const ${mutationOptionsFnName} = <TError = ${errorType},
TContext = unknown>(${mutationArguments}): ${mutationOptionFnReturnType} => {
${
isRequestOptions
? `const {mutation: mutationOptions${
!mutator
? `, axios: axiosOptions`
: mutator?.hasSecondArg
? ', request: requestOptions'
: ''
}} = options ?? {};`
: ''
}
${
isRequestOptions
? `const {mutation: mutationOptions${
!mutator
? `, axios: axiosOptions`
: mutator?.hasSecondArg
? ', request: requestOptions'
: ''
}} = options ?? {};`
: ''
}
${
mutator?.isHook
Expand Down Expand Up @@ -1421,11 +1444,11 @@ const generateQueryHook = async (
}
return ${
!mutationOptionsMutator
? '{ mutationFn, ...mutationOptions }'
: 'customOptions'
}}`;
return ${
!mutationOptionsMutator
? '{ mutationFn, ...mutationOptions }'
: 'customOptions'
}}`;

const operationPrefix = hasSvelteQueryV4 ? 'create' : 'use';

Expand All @@ -1449,7 +1472,11 @@ ${mutationOptionsFn}
${doc}export const ${camel(
`${operationPrefix}-${operationName}`,
)} = <TError = ${errorType},
TContext = unknown>(${mutationArguments}) => {
TContext = unknown>(${mutationArguments})${generateMutatorReturnType({
outputClient,
dataType,
variableType: definitions ? `{${definitions}}` : 'void',
})} => {
const ${mutationOptionsVarName} = ${mutationOptionsFnName}(${
isRequestOptions ? 'options' : 'mutationOptions'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type {
UseInfiniteQueryOptions,
UseInfiniteQueryResult,
UseMutationOptions,
UseMutationResult,
UseQueryOptions,
UseQueryResult,
UseSuspenseInfiniteQueryOptions,
Expand Down Expand Up @@ -476,7 +477,12 @@ export const useCreatePets = <
{ data: CreatePetsBody; version?: number },
TContext
>;
}) => {
}): UseMutationResult<
Awaited<ReturnType<typeof createPets>>,
TError,
{ data: CreatePetsBody; version?: number },
TContext
> => {
const mutationOptions = getCreatePetsMutationOptions(options);

return useMutation(mutationOptions);
Expand Down Expand Up @@ -543,7 +549,12 @@ export const useUpdatePets = <
{ data: NonReadonly<Pet>; version?: number },
TContext
>;
}) => {
}): UseMutationResult<
Awaited<ReturnType<typeof updatePets>>,
TError,
{ data: NonReadonly<Pet>; version?: number },
TContext
> => {
const mutationOptions = getUpdatePetsMutationOptions(options);

return useMutation(mutationOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
QueryFunction,
QueryKey,
UseMutationOptions,
UseMutationResult,
UseQueryOptions,
UseQueryResult,
} from 'react-query';
Expand Down Expand Up @@ -191,7 +192,12 @@ export const useCreatePets = <
{ data: BodyType<CreatePetsBody>; version?: number },
TContext
>;
}) => {
}): UseMutationResult<
Awaited<ReturnType<ReturnType<typeof useCreatePetsHook>>>,
TError,
{ data: BodyType<CreatePetsBody>; version?: number },
TContext
> => {
const mutationOptions = useCreatePetsMutationOptions(options);

return useMutation(mutationOptions);
Expand Down
13 changes: 11 additions & 2 deletions samples/react-query/form-data-mutator/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
* OpenAPI spec version: 1.0.0
*/
import { useMutation } from 'react-query';
import type { MutationFunction, UseMutationOptions } from 'react-query';
import type {
MutationFunction,
UseMutationOptions,
UseMutationResult,
} from 'react-query';
import type { CreatePetsBody, Error, Pet } from './models';
import { customInstance } from './custom-instance';
import { customFormData } from './custom-form-data';
Expand Down Expand Up @@ -73,7 +77,12 @@ export const useCreatePets = <TError = Error, TContext = unknown>(options?: {
{ data: CreatePetsBody },
TContext
>;
}) => {
}): UseMutationResult<
Awaited<ReturnType<typeof createPets>>,
TError,
{ data: CreatePetsBody },
TContext
> => {
const mutationOptions = getCreatePetsMutationOptions(options);

return useMutation(mutationOptions);
Expand Down
13 changes: 11 additions & 2 deletions samples/react-query/form-data/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
* OpenAPI spec version: 1.0.0
*/
import { useMutation } from 'react-query';
import type { MutationFunction, UseMutationOptions } from 'react-query';
import type {
MutationFunction,
UseMutationOptions,
UseMutationResult,
} from 'react-query';
import type { CreatePetsBody, Error, Pet } from './models';
import { customInstance } from './custom-instance';

Expand Down Expand Up @@ -75,7 +79,12 @@ export const useCreatePets = <TError = Error, TContext = unknown>(options?: {
{ data: CreatePetsBody },
TContext
>;
}) => {
}): UseMutationResult<
Awaited<ReturnType<typeof createPets>>,
TError,
{ data: CreatePetsBody },
TContext
> => {
const mutationOptions = getCreatePetsMutationOptions(options);

return useMutation(mutationOptions);
Expand Down
13 changes: 11 additions & 2 deletions samples/react-query/form-url-encoded-mutator/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
* OpenAPI spec version: 1.0.0
*/
import { useMutation } from 'react-query';
import type { MutationFunction, UseMutationOptions } from 'react-query';
import type {
MutationFunction,
UseMutationOptions,
UseMutationResult,
} from 'react-query';
import type { CreatePetsBody, Error, Pet } from './models';
import { customInstance } from './custom-instance';
import { customFormUrlEncoded } from './custom-form-url-encoded';
Expand Down Expand Up @@ -73,7 +77,12 @@ export const useCreatePets = <TError = Error, TContext = unknown>(options?: {
{ data: CreatePetsBody },
TContext
>;
}) => {
}): UseMutationResult<
Awaited<ReturnType<typeof createPets>>,
TError,
{ data: CreatePetsBody },
TContext
> => {
const mutationOptions = getCreatePetsMutationOptions(options);

return useMutation(mutationOptions);
Expand Down
13 changes: 11 additions & 2 deletions samples/react-query/form-url-encoded/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
* OpenAPI spec version: 1.0.0
*/
import { useMutation } from 'react-query';
import type { MutationFunction, UseMutationOptions } from 'react-query';
import type {
MutationFunction,
UseMutationOptions,
UseMutationResult,
} from 'react-query';
import type { CreatePetsBody, Error, Pet } from './models';
import { customInstance } from './custom-instance';

Expand Down Expand Up @@ -75,7 +79,12 @@ export const useCreatePets = <TError = Error, TContext = unknown>(options?: {
{ data: CreatePetsBody },
TContext
>;
}) => {
}): UseMutationResult<
Awaited<ReturnType<typeof createPets>>,
TError,
{ data: CreatePetsBody },
TContext
> => {
const mutationOptions = getCreatePetsMutationOptions(options);

return useMutation(mutationOptions);
Expand Down
8 changes: 7 additions & 1 deletion samples/react-query/hook-mutator/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
QueryFunction,
QueryKey,
UseMutationOptions,
UseMutationResult,
UseQueryOptions,
UseQueryResult,
} from 'react-query';
Expand Down Expand Up @@ -167,7 +168,12 @@ export const useCreatePets = <TError = Error, TContext = unknown>(options?: {
{ data: CreatePetsBody },
TContext
>;
}) => {
}): UseMutationResult<
Awaited<ReturnType<ReturnType<typeof useCreatePetsHook>>>,
TError,
{ data: CreatePetsBody },
TContext
> => {
const mutationOptions = useCreatePetsMutationOptions(options);

return useMutation(mutationOptions);
Expand Down

0 comments on commit 01f8d22

Please sign in to comment.