Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(query): type return of useMutation query #1215

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 63 additions & 17 deletions packages/query/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ const SVELTE_QUERY_DEPENDENCIES_V3: GeneratorDependency[] = [
{ name: 'UseQueryStoreResult' },
{ name: 'UseInfiniteQueryStoreResult' },
{ name: 'QueryKey' },
{ name: 'CreateMutationResult' },
],
dependency: '@sveltestack/svelte-query',
},
Expand All @@ -113,6 +114,7 @@ const SVELTE_QUERY_DEPENDENCIES: GeneratorDependency[] = [
{ name: 'CreateInfiniteQueryResult' },
{ name: 'QueryKey' },
{ name: 'InfiniteData' },
{ name: 'CreateMutationResult' },
],
dependency: '@tanstack/svelte-query',
},
Expand Down Expand Up @@ -160,6 +162,7 @@ const REACT_QUERY_DEPENDENCIES_V3: GeneratorDependency[] = [
{ name: 'UseInfiniteQueryResult' },
{ name: 'QueryKey' },
{ name: 'QueryClient' },
{ name: 'UseMutationResult' },
],
dependency: 'react-query',
},
Expand All @@ -186,6 +189,7 @@ const REACT_QUERY_DEPENDENCIES: GeneratorDependency[] = [
{ name: 'QueryKey' },
{ name: 'QueryClient' },
{ name: 'InfiniteData' },
{ name: 'UseMutationResult' },
],
dependency: '@tanstack/react-query',
},
Expand Down Expand Up @@ -231,6 +235,7 @@ const VUE_QUERY_DEPENDENCIES_V3: GeneratorDependency[] = [
{ name: 'UseQueryResult' },
{ name: 'UseInfiniteQueryResult' },
{ name: 'QueryKey' },
{ name: 'UseMutationReturnType' },
],
dependency: 'vue-query/types',
},
Expand Down Expand Up @@ -262,6 +267,7 @@ const VUE_QUERY_DEPENDENCIES: GeneratorDependency[] = [
{ name: 'UseQueryReturnType' },
{ name: 'UseInfiniteQueryReturnType' },
{ name: 'InfiniteData' },
{ name: 'UseMutationReturnType' },
],
dependency: '@tanstack/vue-query',
},
Expand Down Expand Up @@ -728,6 +734,42 @@ const generateQueryReturnType = ({
}
};

const generateMutatorReturnType = ({
outputClient,
dataType,
variableType,
}: {
outputClient: OutputClient | OutputClientFunc;
dataType: unknown;
variableType: unknown;
}) => {
if (outputClient === OutputClient.REACT_QUERY) {
return `: UseMutationResult<
Awaited<ReturnType<${dataType}>>,
TError,
${variableType},
TContext
>`;
}
if (outputClient === OutputClient.SVELTE_QUERY) {
return `: CreateMutationResult<
Awaited<ReturnType<${dataType}>>,
TError,
${variableType},
TContext
>`;
}
if (outputClient === OutputClient.VUE_QUERY) {
return `: UseMutationReturnType<
Awaited<ReturnType<${dataType}>>,
TError,
${variableType},
TContext
>`;
}
return '';
};

const getQueryOptions = ({
isRequestOptions,
mutator,
Expand Down Expand Up @@ -1382,17 +1424,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 @@ -1430,11 +1472,11 @@ const generateQueryHook = async (
}


return ${
!mutationOptionsMutator
? '{ mutationFn, ...mutationOptions }'
: 'customOptions'
}}`;
return ${
!mutationOptionsMutator
? '{ mutationFn, ...mutationOptions }'
: 'customOptions'
}}`;

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

Expand All @@ -1458,7 +1500,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 @@ -64,9 +64,7 @@ export const useListPets = <TError = Error>(
const query = useSwr<Awaited<ReturnType<typeof swrFn>>, TError>(
swrKey,
swrFn,
{
...swrOptions,
},
swrOptions,
);

return {
Expand Down Expand Up @@ -173,9 +171,7 @@ export const useShowPetById = <TError = Error>(
const query = useSwr<Awaited<ReturnType<typeof swrFn>>, TError>(
swrKey,
swrFn,
{
...swrOptions,
},
swrOptions,
);

return {
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { createMutation, createQuery } from '@tanstack/svelte-query';
import type {
CreateMutationOptions,
CreateMutationResult,
CreateQueryOptions,
CreateQueryResult,
MutationFunction,
Expand Down Expand Up @@ -175,7 +176,12 @@ export const createCreatePets = <TError = Error, TContext = unknown>(options?: {
{ data: CreatePetsBody; version?: number },
TContext
>;
}) => {
}): CreateMutationResult<
Awaited<ReturnType<typeof createPets>>,
TError,
{ data: CreatePetsBody; version?: number },
TContext
> => {
const mutationOptions = getCreatePetsMutationOptions(options);

return createMutation(mutationOptions);
Expand Down
Loading