Skip to content

Commit

Permalink
fix: add empty object in place of variables for more consistent keys d…
Browse files Browse the repository at this point in the history
  • Loading branch information
rliljest committed Nov 18, 2024
1 parent ebb87c7 commit 3794bb1
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 39 deletions.
5 changes: 5 additions & 0 deletions .changeset/many-maps-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/typescript-react-query': patch
---

add empty object in place of variables for more consistent keys
4 changes: 2 additions & 2 deletions dev-test/githunt/types.react-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ export const useCurrentUserForProfileQuery = <TData = CurrentUserForProfileQuery
options?: UseQueryOptions<CurrentUserForProfileQuery, TError, TData>,
) => {
return useQuery<CurrentUserForProfileQuery, TError, TData>(
variables === undefined ? ['CurrentUserForProfile'] : ['CurrentUserForProfile', variables],
variables === undefined ? ['CurrentUserForProfile', {}] : ['CurrentUserForProfile', variables],
fetcher<CurrentUserForProfileQuery, CurrentUserForProfileQueryVariables>(
dataSource.endpoint,
dataSource.fetchParams || {},
Expand All @@ -532,7 +532,7 @@ export const useInfiniteCurrentUserForProfileQuery = <
) => {
return useInfiniteQuery<CurrentUserForProfileQuery, TError, TData>(
variables === undefined
? ['CurrentUserForProfile.infinite']
? ['CurrentUserForProfile.infinite', {}]
: ['CurrentUserForProfile.infinite', variables],
metaData =>
fetcher<CurrentUserForProfileQuery, CurrentUserForProfileQueryVariables>(
Expand Down
4 changes: 2 additions & 2 deletions packages/plugins/typescript/react-query/src/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ export abstract class FetcherRenderer {
const identifier = isSuspense ? 'infiniteSuspense' : 'infinite';
if (config.hasRequiredVariables)
return `['${config.node.name.value}.${identifier}', variables]`;
return `variables === undefined ? ['${config.node.name.value}.${identifier}'] : ['${config.node.name.value}.${identifier}', variables]`;
return `variables === undefined ? ['${config.node.name.value}.${identifier}', {}] : ['${config.node.name.value}.${identifier}', variables]`;
}

public generateInfiniteQueryOutput(config: GenerateConfig, isSuspense = false) {
Expand All @@ -245,7 +245,7 @@ export abstract class FetcherRenderer {
public generateQueryKey(config: GenerateConfig, isSuspense: boolean): string {
const identifier = isSuspense ? `${config.node.name.value}Suspense` : config.node.name.value;
if (config.hasRequiredVariables) return `['${identifier}', variables]`;
return `variables === undefined ? ['${identifier}'] : ['${identifier}', variables]`;
return `variables === undefined ? ['${identifier}', {}] : ['${identifier}', variables]`;
}

public generateQueryOutput(config: GenerateConfig, isSuspense = false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ export const useTestQuery = <
) => {
return useQuery<TestQuery, TError, TData>(
variables === undefined ? ['test'] : ['test', variables],
variables === undefined ? ['test', {}] : ['test', variables],
fetcher<TestQuery, TestQueryVariables>(dataSource.endpoint, dataSource.fetchParams || {}, TestDocument, variables),
options
)};
useTestQuery.getKey = (variables?: TestQueryVariables) => variables === undefined ? ['test'] : ['test', variables];
useTestQuery.getKey = (variables?: TestQueryVariables) => variables === undefined ? ['test', {}] : ['test', variables];
export const TestDocument = \`
mutation test($name: String) {
Expand Down Expand Up @@ -89,12 +89,12 @@ export const useTestQuery = <
) => {
return useQuery<TestQuery, TError, TData>(
variables === undefined ? ['test'] : ['test', variables],
variables === undefined ? ['test', {}] : ['test', variables],
fetcher<TestQuery, TestQueryVariables>(dataSource.endpoint, dataSource.fetchParams || {}, TestDocument, variables),
options
)};
useTestQuery.getKey = (variables?: TestQueryVariables) => variables === undefined ? ['test'] : ['test', variables];
useTestQuery.getKey = (variables?: TestQueryVariables) => variables === undefined ? ['test', {}] : ['test', variables];
export const useInfiniteTestQuery = <
TData = TestQuery,
Expand All @@ -106,12 +106,12 @@ export const useInfiniteTestQuery = <
) => {
return useInfiniteQuery<TestQuery, TError, TData>(
variables === undefined ? ['test.infinite'] : ['test.infinite', variables],
variables === undefined ? ['test.infinite', {}] : ['test.infinite', variables],
(metaData) => fetcher<TestQuery, TestQueryVariables>(dataSource.endpoint, dataSource.fetchParams || {}, TestDocument, {...variables, ...(metaData.pageParam ?? {})})(),
options
)};
useInfiniteTestQuery.getKey = (variables?: TestQueryVariables) => variables === undefined ? ['test.infinite'] : ['test.infinite', variables];
useInfiniteTestQuery.getKey = (variables?: TestQueryVariables) => variables === undefined ? ['test.infinite', {}] : ['test.infinite', variables];
export const TestDocument = \`
mutation test($name: String) {
Expand Down Expand Up @@ -166,7 +166,7 @@ export const useTestQuery = <
) => {
return useQuery<TestQuery, TError, TData>(
variables === undefined ? ['test'] : ['test', variables],
variables === undefined ? ['test', {}] : ['test', variables],
fetcher<TestQuery, TestQueryVariables>(dataSource.endpoint, dataSource.fetchParams || {}, TestDocument, variables),
options
)};
Expand Down Expand Up @@ -226,7 +226,7 @@ export const useTestQuery = <
) => {
return useQuery<TestQuery, TError, TData>(
variables === undefined ? ['test'] : ['test', variables],
variables === undefined ? ['test', {}] : ['test', variables],
fetcher<TestQuery, TestQueryVariables>(dataSource.endpoint, dataSource.fetchParams || {}, TestDocument, variables),
options
)};
Expand All @@ -243,7 +243,7 @@ export const useInfiniteTestQuery = <
) => {
return useInfiniteQuery<TestQuery, TError, TData>(
variables === undefined ? ['test.infinite'] : ['test.infinite', variables],
variables === undefined ? ['test.infinite', {}] : ['test.infinite', variables],
(metaData) => fetcher<TestQuery, TestQueryVariables>(dataSource.endpoint, dataSource.fetchParams || {}, TestDocument, {...variables, ...(metaData.pageParam ?? {})})(),
options
)};
Expand Down Expand Up @@ -302,7 +302,7 @@ export const useTestQuery = <
) => {
return useQuery<TTestQuery, TError, TData>(
variables === undefined ? ['test'] : ['test', variables],
variables === undefined ? ['test', {}] : ['test', variables],
useCustomFetcher<TTestQuery, TTestQueryVariables>(TestDocument).bind(null, variables),
options
)};
Expand All @@ -316,7 +316,7 @@ export const useInfiniteTestQuery = <
) => {
const query = useCustomFetcher<TTestQuery, TTestQueryVariables>(TestDocument)
return useInfiniteQuery<TTestQuery, TError, TData>(
variables === undefined ? ['test.infinite'] : ['test.infinite', variables],
variables === undefined ? ['test.infinite', {}] : ['test.infinite', variables],
(metaData) => query({...variables, ...(metaData.pageParam ?? {})}),
options
)};
Expand Down Expand Up @@ -377,7 +377,7 @@ export const useTestQuery = <
) => {
return useQuery<TTestQuery, TError, TData>(
variables === undefined ? ['test'] : ['test', variables],
variables === undefined ? ['test', {}] : ['test', variables],
myCustomFetcher<TTestQuery, TTestQueryVariables>(TestDocument, variables),
options
)};
Expand All @@ -391,7 +391,7 @@ export const useInfiniteTestQuery = <
) => {
return useInfiniteQuery<TTestQuery, TError, TData>(
variables === undefined ? ['test.infinite'] : ['test.infinite', variables],
variables === undefined ? ['test.infinite', {}] : ['test.infinite', variables],
(metaData) => myCustomFetcher<TTestQuery, TTestQueryVariables>(TestDocument, {...variables, ...(metaData.pageParam ?? {})})(),
options
)};
Expand Down Expand Up @@ -452,7 +452,7 @@ export const useTestQuery = <
) => {
return useQuery<TTestQuery, TError, TData>(
variables === undefined ? ['test'] : ['test', variables],
variables === undefined ? ['test', {}] : ['test', variables],
myCustomFetcher<TTestQuery, TTestQueryVariables>(TestDocument, variables),
options
)};
Expand Down Expand Up @@ -514,7 +514,7 @@ export const useTestQuery = <
) => {
return useQuery<TTestQuery, TError, TData>(
variables === undefined ? ['test'] : ['test', variables],
variables === undefined ? ['test', {}] : ['test', variables],
fetcher<TTestQuery, TTestQueryVariables>(dataSource.endpoint, dataSource.fetchParams || {}, TestDocument, variables),
options
)};
Expand Down Expand Up @@ -599,7 +599,7 @@ export const useTestQuery = <
) => {
return useQuery<TTestQuery, TError, TData>(
variables === undefined ? ['test'] : ['test', variables],
variables === undefined ? ['test', {}] : ['test', variables],
fetcher<TTestQuery, TTestQueryVariables>(client, TestDocument, variables, headers),
options
)};
Expand All @@ -615,7 +615,7 @@ export const useInfiniteTestQuery = <
) => {
return useInfiniteQuery<TTestQuery, TError, TData>(
variables === undefined ? ['test.infinite'] : ['test.infinite', variables],
variables === undefined ? ['test.infinite', {}] : ['test.infinite', variables],
(metaData) => fetcher<TTestQuery, TTestQueryVariables>(client, TestDocument, {...variables, ...(metaData.pageParam ?? {})}, headers)(),
options
)};
Expand Down Expand Up @@ -690,7 +690,7 @@ export const useTestQuery = <
) => {
return useQuery<TTestQuery, TError, TData>(
variables === undefined ? ['test'] : ['test', variables],
variables === undefined ? ['test', {}] : ['test', variables],
fetcher<TTestQuery, TTestQueryVariables>(TestDocument, variables, headers),
options
)};
Expand Down Expand Up @@ -764,7 +764,7 @@ export const useTestQuery = <
) => {
return useQuery<TTestQuery, TError, TData>(
variables === undefined ? ['test'] : ['test', variables],
variables === undefined ? ['test', {}] : ['test', variables],
fetcher<TTestQuery, TTestQueryVariables>(TestDocument, variables),
options
)};
Expand Down Expand Up @@ -844,7 +844,7 @@ export const useTestQuery = <
) => {
return useQuery<TTestQuery, TError, TData>(
variables === undefined ? ['test'] : ['test', variables],
variables === undefined ? ['test', {}] : ['test', variables],
fetcher<TTestQuery, TTestQueryVariables>(TestDocument, variables),
options
)};
Expand Down Expand Up @@ -924,7 +924,7 @@ export const useTestQuery = <
) => {
return useQuery<TTestQuery, TError, TData>(
variables === undefined ? ['test'] : ['test', variables],
variables === undefined ? ['test', {}] : ['test', variables],
fetcher<TTestQuery, TTestQueryVariables>(TestDocument, variables),
options
)};
Expand Down Expand Up @@ -1003,7 +1003,7 @@ export const useTestQuery = <
) => {
return useQuery<TTestQuery, TError, TData>(
variables === undefined ? ['test'] : ['test', variables],
variables === undefined ? ['test', {}] : ['test', variables],
fetcher<TTestQuery, TTestQueryVariables>(TestDocument, variables),
options
)};
Expand Down Expand Up @@ -1082,7 +1082,7 @@ export const useTestQuery = <
) => {
return useQuery<TTestQuery, TError, TData>(
variables === undefined ? ['test'] : ['test', variables],
variables === undefined ? ['test', {}] : ['test', variables],
fetcher<TTestQuery, TTestQueryVariables>(TestDocument, variables),
options
)};
Expand All @@ -1096,7 +1096,7 @@ export const useInfiniteTestQuery = <
) => {
return useInfiniteQuery<TTestQuery, TError, TData>(
variables === undefined ? ['test.infinite'] : ['test.infinite', variables],
variables === undefined ? ['test.infinite', {}] : ['test.infinite', variables],
(metaData) => fetcher<TTestQuery, TTestQueryVariables>(TestDocument, {...variables, ...(metaData.pageParam ?? {})})(),
options
)};
Expand Down Expand Up @@ -1202,7 +1202,7 @@ export const useTestQuery = <
) => {
return useQuery<Types.TestQuery, TError, TData>(
variables === undefined ? ['test'] : ['test', variables],
variables === undefined ? ['test', {}] : ['test', variables],
fetcher<Types.TestQuery, Types.TestQueryVariables>(dataSource.endpoint, dataSource.fetchParams || {}, TestDocument, variables),
options
)};
Expand Down Expand Up @@ -1261,7 +1261,7 @@ export const useTestQuery = <
return useQuery<Types.TestQuery, TError, TData>(
{
queryKey: variables === undefined ? ['test'] : ['test', variables],
queryKey: variables === undefined ? ['test', {}] : ['test', variables],
queryFn: fetcher<Types.TestQuery, Types.TestQueryVariables>(dataSource.endpoint, dataSource.fetchParams || {}, TestDocument, variables),
...options
}
Expand Down Expand Up @@ -1322,7 +1322,7 @@ export const useTestQuery = <
) => {
return useQuery<TestQuery, TError, TData>(
variables === undefined ? ['test'] : ['test', variables],
variables === undefined ? ['test', {}] : ['test', variables],
fetcher<TestQuery, TestQueryVariables>(dataSource.endpoint, dataSource.fetchParams || {}, TestDocument, variables),
options
)};
Expand All @@ -1337,7 +1337,7 @@ export const useInfiniteTestQuery = <
) => {
return useInfiniteQuery<TestQuery, TError, TData>(
variables === undefined ? ['test.infinite'] : ['test.infinite', variables],
variables === undefined ? ['test.infinite', {}] : ['test.infinite', variables],
(metaData) => fetcher<TestQuery, TestQueryVariables>(dataSource.endpoint, dataSource.fetchParams || {}, TestDocument, {...variables, ...(metaData.pageParam ?? {})})(),
options
)};
Expand Down Expand Up @@ -1422,7 +1422,7 @@ export const useTestQuery = <
return useQuery<TestQuery, TError, TData>(
{
queryKey: variables === undefined ? ['test'] : ['test', variables],
queryKey: variables === undefined ? ['test', {}] : ['test', variables],
queryFn: fetcher<TestQuery, TestQueryVariables>(dataSource.endpoint, dataSource.fetchParams || {}, TestDocument, variables),
...options
}
Expand All @@ -1439,7 +1439,7 @@ export const useSuspenseTestQuery = <
return useSuspenseQuery<TestQuery, TError, TData>(
{
queryKey: variables === undefined ? ['testSuspense'] : ['testSuspense', variables],
queryKey: variables === undefined ? ['testSuspense', {}] : ['testSuspense', variables],
queryFn: fetcher<TestQuery, TestQueryVariables>(dataSource.endpoint, dataSource.fetchParams || {}, TestDocument, variables),
...options
}
Expand All @@ -1458,7 +1458,7 @@ export const useInfiniteTestQuery = <
(() => {
const { queryKey: optionsQueryKey, ...restOptions } = options;
return {
queryKey: optionsQueryKey ?? variables === undefined ? ['test.infinite'] : ['test.infinite', variables],
queryKey: optionsQueryKey ?? variables === undefined ? ['test.infinite', {}] : ['test.infinite', variables],
queryFn: (metaData) => fetcher<TestQuery, TestQueryVariables>(dataSource.endpoint, dataSource.fetchParams || {}, TestDocument, {...variables, ...(metaData.pageParam ?? {})})(),
...restOptions
}
Expand All @@ -1478,7 +1478,7 @@ export const useSuspenseInfiniteTestQuery = <
(() => {
const { queryKey: optionsQueryKey, ...restOptions } = options;
return {
queryKey: optionsQueryKey ?? variables === undefined ? ['test.infiniteSuspense'] : ['test.infiniteSuspense', variables],
queryKey: optionsQueryKey ?? variables === undefined ? ['test.infiniteSuspense', {}] : ['test.infiniteSuspense', variables],
queryFn: (metaData) => fetcher<TestQuery, TestQueryVariables>(dataSource.endpoint, dataSource.fetchParams || {}, TestDocument, {...variables, ...(metaData.pageParam ?? {})})(),
...restOptions
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,7 @@ describe('React-Query', () => {
const out = (await plugin(schema, docs, config)) as Types.ComplexPluginOutput;
expect(out.content).toMatchSnapshot();
expect(out.content).toBeSimilarStringTo(
`useTestQuery.getKey = (variables?: TestQueryVariables) => variables === undefined ? ['test'] : ['test', variables];`,
`useTestQuery.getKey = (variables?: TestQueryVariables) => variables === undefined ? ['test', {}] : ['test', variables];`,
);
});
});
Expand All @@ -980,10 +980,10 @@ describe('React-Query', () => {
const out = (await plugin(schema, docs, config)) as Types.ComplexPluginOutput;
expect(out.content).toMatchSnapshot();
expect(out.content).toBeSimilarStringTo(
`useTestQuery.getKey = (variables?: TestQueryVariables) => variables === undefined ? ['test'] : ['test', variables];`,
`useTestQuery.getKey = (variables?: TestQueryVariables) => variables === undefined ? ['test', {}] : ['test', variables];`,
);
expect(out.content).toBeSimilarStringTo(
`useInfiniteTestQuery.getKey = (variables?: TestQueryVariables) => variables === undefined ? ['test.infinite'] : ['test.infinite', variables];`,
`useInfiniteTestQuery.getKey = (variables?: TestQueryVariables) => variables === undefined ? ['test.infinite', {}] : ['test.infinite', variables];`,
);
});
});
Expand Down

0 comments on commit 3794bb1

Please sign in to comment.