Skip to content
This repository was archived by the owner on May 19, 2023. It is now read-only.

Updated trpc query keys format for RSC to correspond to how trpc v10.0 format them #39

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions @trpc/next-layout/createTRPCNextLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ export type DecorateProcedure<TProcedure extends AnyProcedure> =
TProcedure extends AnyQueryProcedure
? {
fetch(
input: inferProcedureInput<TProcedure>,
input: inferProcedureInput<TProcedure>
): Promise<inferProcedureOutput<TProcedure>>;
fetchInfinite(
input: inferProcedureInput<TProcedure>,
input: inferProcedureInput<TProcedure>
): Promise<inferProcedureOutput<TProcedure>>;
}
: never;
Expand All @@ -47,7 +47,7 @@ type OmitNever<TType> = Pick<
*/
export type DecoratedProcedureRecord<
TProcedures extends ProcedureRouterRecord,
TPath extends string = "",
TPath extends string = ""
> = OmitNever<{
[TKey in keyof TProcedures]: TProcedures[TKey] extends AnyRouter
? DecoratedProcedureRecord<
Expand All @@ -65,12 +65,24 @@ type CreateTRPCNextLayout<TRouter extends AnyRouter> = DecoratedProcedureRecord<
dehydrate(): Promise<DehydratedState>;
};

function getQueryKey(path: string[], input: unknown) {
return input === undefined ? [path] : [path, input];
function getQueryKey(
path: string[],
input: unknown,
isFetchInfinite?: boolean
) {
return input === undefined
? [path, { type: isFetchInfinite ? "infinite" : "query" }] // We added { type: "infinite" | "query" }, because it is how trpc v10.0 format the new queryKeys
: [
path,
{
input: { ...input },
type: isFetchInfinite ? "infinite" : "query",
},
];
}

export function createTRPCNextLayout<TRouter extends AnyRouter>(
opts: CreateTRPCNextLayoutOptions<TRouter>,
opts: CreateTRPCNextLayoutOptions<TRouter>
): CreateTRPCNextLayout<TRouter> {
function getState() {
const requestStorage = getRequestStorage<{
Expand Down Expand Up @@ -117,11 +129,11 @@ export function createTRPCNextLayout<TRouter extends AnyRouter>(

const pathStr = path.join(".");
const input = callOpts.args[0];
const queryKey = getQueryKey(path, input);
const queryKey = getQueryKey(path, input, lastPart === "fetchInfinite");

if (lastPart === "fetchInfinite") {
return queryClient.fetchInfiniteQuery(queryKey, () =>
caller.query(pathStr, input),
caller.query(pathStr, input)
);
}

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
"@tailwindcss/typography": "^0.5.7",
"@tanstack/query-core": "^4.14.5",
"@tanstack/react-query": "^4.14.5",
"@trpc/client": "^10.0.0-rc.1",
"@trpc/react-query": "^10.0.0-rc.1",
"@trpc/server": "^10.0.0-rc.1",
"@trpc/client": "^10.0.0",
"@trpc/react-query": "^10.0.0",
"@trpc/server": "^10.0.0",
"next": "^13.0.1-canary.3",
"next-auth": "4.14.0",
"npm-run-all": "^4.1.5",
Expand Down