Skip to content

Commit

Permalink
fix: infiniteData type
Browse files Browse the repository at this point in the history
  • Loading branch information
neil585456525 committed Oct 15, 2023
1 parent b22c111 commit a36fe2a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
15 changes: 14 additions & 1 deletion packages/plugins/typescript/react-query/src/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type ReactQueryMethodMap = {
[key: string]: {
getHook: (operationName?: string) => string;
getOptions: () => string;
getOtherTypes?: () => { [key: string]: string };
};
};

Expand All @@ -44,6 +45,7 @@ export abstract class FetcherRenderer {
infiniteQuery: {
getHook: (operationName = 'Query') => `use${suspenseText}Infinite${operationName}`,
getOptions: () => `Use${suspenseText}InfiniteQueryOptions`,
getOtherTypes: () => ({ infiniteData: 'InfiniteData' }),
},
query: {
getHook: (operationName = 'Query') => `use${suspenseText}${operationName}`,
Expand All @@ -63,8 +65,15 @@ export abstract class FetcherRenderer {

const { infiniteQuery } = this.createQueryMethodMap(isSuspense);

const isNextVersion = this.visitor.config.reactQueryVersion >= 5;

this.visitor.reactQueryHookIdentifiersInUse.add(infiniteQuery.getHook());
this.visitor.reactQueryOptionsIdentifiersInUse.add(infiniteQuery.getOptions());
if (isNextVersion) {
this.visitor.reactQueryOptionsIdentifiersInUse.add(
infiniteQuery.getOtherTypes().infiniteData,
);
}

const variables = this.generateInfiniteQueryVariablesSignature(config);
const options = this.generateInfiniteQueryOptionsSignature(config, isSuspense);
Expand All @@ -80,7 +89,11 @@ export abstract class FetcherRenderer {
`;

return `export const ${infiniteQuery.getHook(operationName)} = <
TData = ${operationResultType},
TData = ${
isNextVersion
? `${infiniteQuery.getOtherTypes().infiniteData}<${operationResultType}>`
: operationResultType
},
TError = ${this.visitor.config.errorType}
>(${argumentsResult}) => {
${implHookOuter}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1272,7 +1272,7 @@ function fetcher<TData, TVariables>(endpoint: string, requestInit: RequestInit,
]
`;
exports[`React-Query support v5 syntax 1`] = `
exports[`React-Query support v5 syntax: content 1`] = `
"
export const TestDocument = \`
Expand Down Expand Up @@ -1326,7 +1326,7 @@ export const useSuspenseTestQuery = <
)};
export const useInfiniteTestQuery = <
TData = TestQuery,
TData = InfiniteData<TestQuery>,
TError = unknown
>(
dataSource: { endpoint: string, fetchParams?: RequestInit },
Expand All @@ -1346,7 +1346,7 @@ export const useInfiniteTestQuery = <
)};
export const useSuspenseInfiniteTestQuery = <
TData = TestQuery,
TData = InfiniteData<TestQuery>,
TError = unknown
>(
dataSource: { endpoint: string, fetchParams?: RequestInit },
Expand Down Expand Up @@ -1390,3 +1390,29 @@ export const useTestMutation = <
)};
"
`;
exports[`React-Query support v5 syntax: prepend 1`] = `
[
"import { useQuery, useSuspenseQuery, useInfiniteQuery, useSuspenseInfiniteQuery, useMutation, UseQueryOptions, UseSuspenseQueryOptions, UseInfiniteQueryOptions, InfiniteData, UseSuspenseInfiniteQueryOptions, UseMutationOptions } from '@tanstack/react-query';",
"
function fetcher<TData, TVariables>(endpoint: string, requestInit: RequestInit, query: string, variables?: TVariables) {
return async (): Promise<TData> => {
const res = await fetch(endpoint, {
method: 'POST',
...requestInit,
body: JSON.stringify({ query, variables }),
});
const json = await res.json();
if (json.errors) {
const { message } = json.errors[0];
throw new Error(message);
}
return json.data;
}
}",
]
`;
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ describe('React-Query', () => {

const out = (await plugin(schema, docs, config)) as Types.ComplexPluginOutput;

expect(out.content).toMatchSnapshot();
expect(out.prepend).toMatchSnapshot('prepend');
expect(out.content).toMatchSnapshot('content');
await validateTypeScript(mergeOutputs(out), schema, docs, config);
});

Expand Down

0 comments on commit a36fe2a

Please sign in to comment.