Skip to content

Commit

Permalink
feat(code-gen): generate RQ5 like object arguments while staying on RQ4
Browse files Browse the repository at this point in the history
References #2644
  • Loading branch information
dirkdev98 committed Oct 26, 2023
1 parent 96554cc commit 1276993
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions packages/code-gen/src/api-client/react-query.js
Expand Up @@ -454,13 +454,14 @@ export function reactQueryGenerateFunction(

fileWriteInline(
file,
`return useQuery(${hookName}.queryKey(${
routeHasMandatoryInputs ? "opts" : ""
}),`,
`return useQuery({
queryKey: ${hookName}.queryKey(${
routeHasMandatoryInputs ? "opts" : ""
}),`,
);
fileWrite(
file,
`({ signal }) => {
`queryFn: ({ signal }) => {
${reactQueryCheckIfRequiredVariablesArePresent(
generateContext,
hookName,
Expand All @@ -477,7 +478,7 @@ return ${apiName}(${apiInstanceParameter}
defaultToNull: false,
})}
);
}, options);`,
}, ...options });`,
);

fileBlockEnd(file);
Expand Down Expand Up @@ -523,9 +524,9 @@ ${hookName}.queryKey = (
requireAllParams: false,
})}
) => {
return queryClient.fetchQuery(
${hookName}.queryKey(${routeHasMandatoryInputs ? "opts" : ""}),
() => {
return queryClient.fetchQuery({
queryKey: ${hookName}.queryKey(${routeHasMandatoryInputs ? "opts" : ""}),
queryFn: () => {
${reactQueryCheckIfRequiredVariablesArePresent(
generateContext,
`${hookName}.fetchQuery`,
Expand All @@ -538,7 +539,7 @@ ${hookName}.queryKey = (
withRequestConfig: true,
defaultToNull: false,
})}
); });
); }, });
}
/**
Expand All @@ -553,9 +554,9 @@ ${hookName}.queryKey = (
requireAllParams: false,
})},
) => {
return queryClient.prefetchQuery(
${hookName}.queryKey(${routeHasMandatoryInputs ? "opts" : ""}),
() => {
return queryClient.prefetchQuery({
queryKey: ${hookName}.queryKey(${routeHasMandatoryInputs ? "opts" : ""}),
queryFn: () => {
${reactQueryCheckIfRequiredVariablesArePresent(
generateContext,
`${hookName}.prefetchQuery`,
Expand All @@ -570,7 +571,7 @@ ${hookName}.queryKey = (
defaultToNull: false,
})}
);
});
}, });
}
/**
Expand All @@ -587,9 +588,9 @@ ${hookName}.invalidate = (
})},`
: ""
}
) => queryClient.invalidateQueries(${hookName}.queryKey(${
) => queryClient.invalidateQueries({ queryKey: ${hookName}.queryKey(${
routeHasMandatoryInputs ? "opts" : ""
}));
}) });
/**
Expand Down Expand Up @@ -636,9 +637,9 @@ ${hookName}.setQueryData = (

fileWriteInline(
file,
`export function ${hookName}(options: UseMutationOptions<${defaultedResponseType}, AppErrorResponse, ${upperCaseFirst(
`export function ${hookName}<Context = unknown>(options: UseMutationOptions<${defaultedResponseType}, AppErrorResponse, ${upperCaseFirst(
hookName,
)}Props> = {},`,
)}Props, Context> = {},`,
);

if (route.invalidations.length > 0) {
Expand All @@ -653,7 +654,7 @@ ${hookName}.setQueryData = (
file,
`): UseMutationResult<${defaultedResponseType}, AppErrorResponse, ${upperCaseFirst(
hookName,
)}Props, unknown> {`,
)}Props, Context> {`,
);

if (!distilledTargetInfo.useGlobalClients) {
Expand Down Expand Up @@ -681,14 +682,14 @@ ${hookName}.setQueryData = (

fileWrite(
file,
`return useMutation((variables) => ${apiName}(
`return useMutation({ mutationFn: (variables) => ${apiName}(
${apiInstanceParameter}
${parameterListWithExtraction({
prefix: "variables",
withRequestConfig: true,
defaultToNull: false,
})}
), options);
), ...options });
`,
);

Expand Down Expand Up @@ -839,7 +840,7 @@ function reactQueryWriteInvalidations(file, route) {

fileWriteInline(
file,
`queryClient.invalidateQueries(["${invalidation.target.group}",`,
`queryClient.invalidateQueries({ queryKey: ["${invalidation.target.group}",`,
);
if (invalidation.target.name) {
fileWriteInline(file, `"${invalidation.target.name}",`);
Expand All @@ -850,7 +851,7 @@ function reactQueryWriteInvalidations(file, route) {
if (query.length) {
fileWriteInline(file, query);
}
fileWrite(file, `]);`);
fileWrite(file, `] });`);
}

fileBlockStart(file, `if (typeof originalOnSuccess === "function")`);
Expand Down

0 comments on commit 1276993

Please sign in to comment.