Skip to content

Commit 1276993

Browse files
committed
feat(code-gen): generate RQ5 like object arguments while staying on RQ4
References #2644
1 parent 96554cc commit 1276993

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

packages/code-gen/src/api-client/react-query.js

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -454,13 +454,14 @@ export function reactQueryGenerateFunction(
454454

455455
fileWriteInline(
456456
file,
457-
`return useQuery(${hookName}.queryKey(${
458-
routeHasMandatoryInputs ? "opts" : ""
459-
}),`,
457+
`return useQuery({
458+
queryKey: ${hookName}.queryKey(${
459+
routeHasMandatoryInputs ? "opts" : ""
460+
}),`,
460461
);
461462
fileWrite(
462463
file,
463-
`({ signal }) => {
464+
`queryFn: ({ signal }) => {
464465
${reactQueryCheckIfRequiredVariablesArePresent(
465466
generateContext,
466467
hookName,
@@ -477,7 +478,7 @@ return ${apiName}(${apiInstanceParameter}
477478
defaultToNull: false,
478479
})}
479480
);
480-
}, options);`,
481+
}, ...options });`,
481482
);
482483

483484
fileBlockEnd(file);
@@ -523,9 +524,9 @@ ${hookName}.queryKey = (
523524
requireAllParams: false,
524525
})}
525526
) => {
526-
return queryClient.fetchQuery(
527-
${hookName}.queryKey(${routeHasMandatoryInputs ? "opts" : ""}),
528-
() => {
527+
return queryClient.fetchQuery({
528+
queryKey: ${hookName}.queryKey(${routeHasMandatoryInputs ? "opts" : ""}),
529+
queryFn: () => {
529530
${reactQueryCheckIfRequiredVariablesArePresent(
530531
generateContext,
531532
`${hookName}.fetchQuery`,
@@ -538,7 +539,7 @@ ${hookName}.queryKey = (
538539
withRequestConfig: true,
539540
defaultToNull: false,
540541
})}
541-
); });
542+
); }, });
542543
}
543544
544545
/**
@@ -553,9 +554,9 @@ ${hookName}.queryKey = (
553554
requireAllParams: false,
554555
})},
555556
) => {
556-
return queryClient.prefetchQuery(
557-
${hookName}.queryKey(${routeHasMandatoryInputs ? "opts" : ""}),
558-
() => {
557+
return queryClient.prefetchQuery({
558+
queryKey: ${hookName}.queryKey(${routeHasMandatoryInputs ? "opts" : ""}),
559+
queryFn: () => {
559560
${reactQueryCheckIfRequiredVariablesArePresent(
560561
generateContext,
561562
`${hookName}.prefetchQuery`,
@@ -570,7 +571,7 @@ ${hookName}.queryKey = (
570571
defaultToNull: false,
571572
})}
572573
);
573-
});
574+
}, });
574575
}
575576
576577
/**
@@ -587,9 +588,9 @@ ${hookName}.invalidate = (
587588
})},`
588589
: ""
589590
}
590-
) => queryClient.invalidateQueries(${hookName}.queryKey(${
591+
) => queryClient.invalidateQueries({ queryKey: ${hookName}.queryKey(${
591592
routeHasMandatoryInputs ? "opts" : ""
592-
}));
593+
}) });
593594
594595
595596
/**
@@ -636,9 +637,9 @@ ${hookName}.setQueryData = (
636637

637638
fileWriteInline(
638639
file,
639-
`export function ${hookName}(options: UseMutationOptions<${defaultedResponseType}, AppErrorResponse, ${upperCaseFirst(
640+
`export function ${hookName}<Context = unknown>(options: UseMutationOptions<${defaultedResponseType}, AppErrorResponse, ${upperCaseFirst(
640641
hookName,
641-
)}Props> = {},`,
642+
)}Props, Context> = {},`,
642643
);
643644

644645
if (route.invalidations.length > 0) {
@@ -653,7 +654,7 @@ ${hookName}.setQueryData = (
653654
file,
654655
`): UseMutationResult<${defaultedResponseType}, AppErrorResponse, ${upperCaseFirst(
655656
hookName,
656-
)}Props, unknown> {`,
657+
)}Props, Context> {`,
657658
);
658659

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

682683
fileWrite(
683684
file,
684-
`return useMutation((variables) => ${apiName}(
685+
`return useMutation({ mutationFn: (variables) => ${apiName}(
685686
${apiInstanceParameter}
686687
${parameterListWithExtraction({
687688
prefix: "variables",
688689
withRequestConfig: true,
689690
defaultToNull: false,
690691
})}
691-
), options);
692+
), ...options });
692693
`,
693694
);
694695

@@ -839,7 +840,7 @@ function reactQueryWriteInvalidations(file, route) {
839840

840841
fileWriteInline(
841842
file,
842-
`queryClient.invalidateQueries(["${invalidation.target.group}",`,
843+
`queryClient.invalidateQueries({ queryKey: ["${invalidation.target.group}",`,
843844
);
844845
if (invalidation.target.name) {
845846
fileWriteInline(file, `"${invalidation.target.name}",`);
@@ -850,7 +851,7 @@ function reactQueryWriteInvalidations(file, route) {
850851
if (query.length) {
851852
fileWriteInline(file, query);
852853
}
853-
fileWrite(file, `]);`);
854+
fileWrite(file, `] });`);
854855
}
855856

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

0 commit comments

Comments
 (0)