Skip to content

Commit

Permalink
refactor(query-proxy): simplify infiniteQueryOptions proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Jul 14, 2023
1 parent e4eaac9 commit 78cd9bb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 28 deletions.
1 change: 0 additions & 1 deletion packages/query-proxy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"zod": "^3.21.4"
},
"peerDependencies": {
"@tanstack/query-core": "*",
"@trpc/server": "*"
},
"peerDependenciesMeta": {
Expand Down
23 changes: 8 additions & 15 deletions packages/query-proxy/src/record.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,13 @@ describe(createFnRecordQueryProxy.name, () => {
// infinite query
//

const infiniteQueryObserver = new InfiniteQueryObserver(
queryClient,
fnRecordQuery.getPage.infiniteQueryOptions(
{
limit: 5,
},
{
getNextPageParam: (lastPage) => lastPage.nextCursor,
setPageParam: (input, pageParam) => ({
...input,
cursor: pageParam as any,
}),
}
)
);
const infiniteQueryObserver = new InfiniteQueryObserver(queryClient, {
...fnRecordQuery.getPage.infiniteQueryOptions((context: any) => ({
limit: 5,
cursor: context?.pageParam,
})),
getNextPageParam: (lastPage) => lastPage.nextCursor,
});

expect((await infiniteQueryObserver.fetchNextPage()).data)
.toMatchInlineSnapshot(`
Expand Down Expand Up @@ -248,6 +240,7 @@ describe(createFnRecordQueryProxy.name, () => {
"queryKey": [
"getPage",
{
"cursor": undefined,
"limit": 5,
},
],
Expand Down
19 changes: 7 additions & 12 deletions packages/query-proxy/src/record.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { GetNextPageParamFunction } from "@tanstack/query-core";
import { createGetterProxy } from "./utils";

type FnAny = (...args: any[]) => any;
Expand All @@ -22,15 +21,13 @@ export type FnRecordQueryProxy<T extends FnRecord> = {
queryFn: () => Promise<FnOut<T[K]>>;
};
infiniteQueryOptions: (
input: FnIn<T[K]>,
options: {
getNextPageParam: GetNextPageParamFunction<FnOut<T[K]>>;
setPageParam: (input: FnIn<T[K]>, pageParam: unknown) => FnIn<T[K]>;
}
// `context` is supposed to be `import("@tanstack/query-core").QueryFunctionContext`
// but we don't exactly specify the type to avoid restricting tanstack query version.
// Assuming the use case of infinite query is limited, having "unknown" here won't degrade DX so bad.
inputFn: (context?: unknown) => FnIn<T[K]>
) => {
queryKey: unknown[];
queryFn: (context: unknown) => Promise<FnOut<T[K]>>;
getNextPageParam: any;
};
mutationKey: unknown[];
mutationOptions: () => {
Expand All @@ -55,11 +52,9 @@ export function createFnRecordQueryProxy<T extends FnRecord>(
});
}
if (prop === "infiniteQueryOptions") {
return (input: unknown, options: any) => ({
queryKey: [k, input],
queryFn: ({ pageParam }: any) =>
(fnRecord as any)[k](options.setPageParam(input, pageParam)),
getNextPageParam: options.getNextPageParam,
return (inputFn: any) => ({
queryKey: [k, inputFn()],
queryFn: (context: any) => (fnRecord as any)[k](inputFn(context)),
});
}
if (prop === "mutationOptions") {
Expand Down

0 comments on commit 78cd9bb

Please sign in to comment.