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): infinte query return type and datatag error generic #1804

Closed
Closed
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
72 changes: 62 additions & 10 deletions packages/query/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,21 @@ const isQueryV5 = (
return compareVersions(withoutRc, '5.0.0');
};

const isQueryV5WithDataTagError = (
packageJson: PackageJson | undefined,
queryClient: 'react-query' | 'vue-query' | 'svelte-query',
) => {
const version = getPackageByQueryClient(packageJson, queryClient);

if (!version) {
return false;
}

const withoutRc = version.split('-')[0];

return compareVersions(withoutRc, '5.62.0');
};

const getPackageByQueryClient = (
packageJson: PackageJson | undefined,
queryClient: 'react-query' | 'vue-query' | 'svelte-query',
Expand Down Expand Up @@ -437,6 +452,8 @@ const isSuspenseQuery = (type: QueryType) => {
};

const getQueryOptionsDefinition = ({
operationName,
mutator,
definitions,
type,
hasSvelteQueryV4,
Expand All @@ -446,6 +463,8 @@ const getQueryOptionsDefinition = ({
isReturnType,
initialData,
}: {
operationName: string;
mutator?: GeneratorMutator;
definitions: string;
type?: QueryType;
hasSvelteQueryV4: boolean;
Expand All @@ -459,11 +478,20 @@ const getQueryOptionsDefinition = ({
const partialOptions = !isReturnType && hasQueryV5;

if (type) {
const funcReturnType =
type === QueryType.INFINITE
? `Awaited<ReturnType<${
mutator?.isHook
? `ReturnType<typeof use${pascal(operationName)}Hook>`
: `typeof ${operationName}`
}>>`
: 'TData';

const optionTypeInitialDataPostfix =
initialData && !isSuspenseQuery(type)
? ` & Pick<
${pascal(initialData)}InitialDataOptions<
TData,
${funcReturnType},
TError,
TData${
hasQueryV5 &&
Expand All @@ -477,12 +505,12 @@ const getQueryOptionsDefinition = ({
> , 'initialData'
>`
: '';
const optionType = `${prefix}${pascal(type)}Options<TData, TError, TData${
const optionType = `${prefix}${pascal(type)}Options<${funcReturnType}, TError, TData${
hasQueryV5 &&
(type === QueryType.INFINITE || type === QueryType.SUSPENSE_INFINITE) &&
queryParam &&
queryParams
? `, TData, QueryKey, ${queryParams?.schema.name}['${queryParam}']`
? `, ${funcReturnType}, QueryKey, ${queryParams?.schema.name}['${queryParam}']`
: ''
}>`;
return `${partialOptions ? 'Partial<' : ''}${optionType}${
Expand All @@ -494,6 +522,7 @@ const getQueryOptionsDefinition = ({
};

const generateQueryArguments = ({
operationName,
definitions,
mutator,
isRequestOptions,
Expand All @@ -505,6 +534,7 @@ const generateQueryArguments = ({
initialData,
httpClient,
}: {
operationName: string;
definitions: string;
mutator?: GeneratorMutator;
isRequestOptions: boolean;
Expand All @@ -517,6 +547,8 @@ const generateQueryArguments = ({
httpClient: OutputHttpClient;
}) => {
const definition = getQueryOptionsDefinition({
operationName,
mutator,
definitions,
type,
hasSvelteQueryV4,
Expand Down Expand Up @@ -549,6 +581,7 @@ const generateQueryReturnType = ({
hasVueQueryV4,
hasSvelteQueryV4,
hasQueryV5,
hasQueryV5WithDataTagError,
isInitialDataDefined,
}: {
outputClient: OutputClient | OutputClientFunc;
Expand All @@ -558,6 +591,7 @@ const generateQueryReturnType = ({
hasVueQueryV4: boolean;
hasSvelteQueryV4: boolean;
hasQueryV5: boolean;
hasQueryV5WithDataTagError: boolean;
isInitialDataDefined?: boolean;
}) => {
switch (outputClient) {
Expand All @@ -572,7 +606,7 @@ const generateQueryReturnType = ({

return `Create${pascal(
type,
)}Result<TData, TError> & { queryKey: ${hasQueryV5 ? 'DataTag<QueryKey, TData>' : 'QueryKey'} }`;
)}Result<TData, TError> & { queryKey: ${hasQueryV5 ? `DataTag<QueryKey, TData${hasQueryV5WithDataTagError ? ', TError' : ''}>` : 'QueryKey'} }`;
}
case OutputClient.VUE_QUERY: {
if (!hasVueQueryV4) {
Expand All @@ -582,16 +616,16 @@ const generateQueryReturnType = ({
}

if (type !== QueryType.INFINITE && type !== QueryType.SUSPENSE_INFINITE) {
return `UseQueryReturnType<TData, TError> & { queryKey: ${hasQueryV5 ? 'DataTag<QueryKey, TData>' : 'QueryKey'} }`;
return `UseQueryReturnType<TData, TError> & { queryKey: ${hasQueryV5 ? `DataTag<QueryKey, TData${hasQueryV5WithDataTagError ? ', TError' : ''}>` : 'QueryKey'} }`;
}

return `UseInfiniteQueryReturnType<TData, TError> & { queryKey: ${hasQueryV5 ? 'DataTag<QueryKey, TData>' : 'QueryKey'} }`;
return `UseInfiniteQueryReturnType<TData, TError> & { queryKey: ${hasQueryV5 ? `DataTag<QueryKey, TData${hasQueryV5WithDataTagError ? ', TError' : ''}>` : 'QueryKey'} }`;
}
case OutputClient.REACT_QUERY:
default: {
return ` ${
isInitialDataDefined && !isSuspenseQuery(type) ? 'Defined' : ''
}Use${pascal(type)}Result<TData, TError> & { queryKey: ${hasQueryV5 ? 'DataTag<QueryKey, TData>' : 'QueryKey'} }`;
}Use${pascal(type)}Result<TData, TError> & { queryKey: ${hasQueryV5 ? `DataTag<QueryKey, TData${hasQueryV5WithDataTagError ? ', TError' : ''}>` : 'QueryKey'} }`;
}
}
};
Expand Down Expand Up @@ -674,6 +708,7 @@ const generateQueryImplementation = ({
hasVueQueryV4,
hasSvelteQueryV4,
hasQueryV5,
hasQueryV5WithDataTagError,
doc,
usePrefetch,
}: {
Expand Down Expand Up @@ -703,6 +738,7 @@ const generateQueryImplementation = ({
hasVueQueryV4: boolean;
hasSvelteQueryV4: boolean;
hasQueryV5: boolean;
hasQueryV5WithDataTagError: boolean;
doc?: string;
usePrefetch?: boolean;
}) => {
Expand Down Expand Up @@ -763,6 +799,7 @@ const generateQueryImplementation = ({
hasVueQueryV4,
hasSvelteQueryV4,
hasQueryV5,
hasQueryV5WithDataTagError,
isInitialDataDefined: true,
});
const returnType = generateQueryReturnType({
Expand All @@ -773,6 +810,7 @@ const generateQueryImplementation = ({
hasVueQueryV4,
hasSvelteQueryV4,
hasQueryV5,
hasQueryV5WithDataTagError,
});

const errorType = getQueryErrorType(
Expand All @@ -787,8 +825,9 @@ const generateQueryImplementation = ({
: `typeof ${operationName}`;

const definedInitialDataQueryArguments = generateQueryArguments({
definitions: '',
operationName,
mutator,
definitions: '',
isRequestOptions,
type,
hasSvelteQueryV4,
Expand All @@ -799,6 +838,7 @@ const generateQueryImplementation = ({
httpClient,
});
const undefinedInitialDataQueryArguments = generateQueryArguments({
operationName,
definitions: '',
mutator,
isRequestOptions,
Expand All @@ -811,6 +851,7 @@ const generateQueryImplementation = ({
httpClient,
});
const queryArguments = generateQueryArguments({
operationName,
definitions: '',
mutator,
isRequestOptions,
Expand Down Expand Up @@ -843,6 +884,8 @@ const generateQueryImplementation = ({
});

const queryOptionFnReturnType = getQueryOptionsDefinition({
operationName,
mutator,
definitions: '',
type,
hasSvelteQueryV4,
Expand Down Expand Up @@ -945,7 +988,7 @@ ${hookOptions}
} as ${queryOptionFnReturnType} ${
isVue(outputClient)
? ''
: `& { queryKey: ${hasQueryV5 ? 'DataTag<QueryKey, TData>' : 'QueryKey'} }`
: `& { queryKey: ${hasQueryV5 ? `DataTag<QueryKey, TData${hasQueryV5WithDataTagError ? ', TError' : ''}>` : 'QueryKey'} }`
}
}`;

Expand Down Expand Up @@ -979,7 +1022,7 @@ export function ${queryHookName}<TData = ${TData}, TError = ${errorType}>(\n ${q

${queryResultVarName}.queryKey = ${
isVue(outputClient) ? `unref(${queryOptionsVarName})` : queryOptionsVarName
}.queryKey ${isVue(outputClient) ? `as ${hasQueryV5 ? 'DataTag<QueryKey, TData>' : 'QueryKey'}` : ''};
}.queryKey ${isVue(outputClient) ? `as ${hasQueryV5 ? `DataTag<QueryKey, TData${hasQueryV5WithDataTagError ? ', TError' : ''}>` : 'QueryKey'}` : ''};

return ${queryResultVarName};
}\n
Expand Down Expand Up @@ -1044,6 +1087,11 @@ const generateQueryHook = async (
outputClient as 'react-query' | 'vue-query' | 'svelte-query',
);

const hasQueryV5WithDataTagError = isQueryV5WithDataTagError(
context.output.packageJson,
outputClient as 'react-query' | 'vue-query' | 'svelte-query',
);

const httpClient = context.output.httpClient;
const doc = jsDoc({ summary, deprecated });

Expand Down Expand Up @@ -1208,6 +1256,7 @@ const generateQueryHook = async (
hasVueQueryV4,
hasSvelteQueryV4,
hasQueryV5,
hasQueryV5WithDataTagError,
doc,
usePrefetch: query.usePrefetch,
}),
Expand Down Expand Up @@ -1266,13 +1315,16 @@ const generateQueryHook = async (
: `typeof ${operationName}`;

const mutationOptionFnReturnType = getQueryOptionsDefinition({
operationName,
mutator,
definitions,
hasSvelteQueryV4,
hasQueryV5,
isReturnType: true,
});

const mutationArguments = generateQueryArguments({
operationName,
definitions,
mutator,
isRequestOptions,
Expand Down
3 changes: 2 additions & 1 deletion tests/configs/react-query.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ export default defineConfig({
name: 'customInstance',
},
query: {
useQuery: true,
useSuspenseQuery: true,
useSuspenseInfiniteQuery: true,
useInfinite: true,
useInfiniteQueryParam: 'limit',
},
Expand Down
4 changes: 2 additions & 2 deletions tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
"devDependencies": {
"npm-run-all": "^4.1.5",
"orval": "link:../packages/orval/dist",
"typescript": "4.8.2"
"typescript": "5.3.3"
},
"dependencies": {
"@angular/common": "^17.1.1",
"@angular/core": "^17.1.1",
"@faker-js/faker": "^8.1.0",
"@tanstack/react-query": "^4.22.0",
"@tanstack/react-query": "^5.63.0",
"@tanstack/svelte-query": "^4.24.9",
"@tanstack/vue-query": "^4.22.0",
"@types/react": "18.3.2",
Expand Down
19 changes: 19 additions & 0 deletions tests/regressions/react-query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { useListPetsInfinite } from '../generated/react-query/mutator/endpoints';

export const useInfiniteQueryTest = () => {
const { data } = useListPetsInfinite(
{
sort: 'name',
},
0,
{
query: {
getNextPageParam: (lastPage) => lastPage[0].id.toString(),
},
},
);

const pages = data?.pages.map((page) => page.map((pet) => pet.name)).flat();

return pages;
};
Loading
Loading