tRPCttRPC
Powered by
JokcyJ
tRPC•4y ago
Jokcy

Demo code `trpc.infinitePosts.add` on infinitedQuery not working

Doc here: https://trpc.io/docs/useInfiniteQuery#getinfinitedata, I created an my query like this:
infinitePosts: protectedProcedure
        .input(
            z.object({
                limit: z.number().min(1).max(100).nullish(),
                cursor: z.string().nullish(), // <-- "cursor" needs to exist, but can be any type
            })
        )
        .query(async ({ ctx, input }) => {
            const limit = input.limit ?? 20;
            const { cursor } = input;
            const items = await ctx.prisma.post.findMany({
                take: limit + 1, // get an extra item at the end which we'll use as next cursor
                cursor: cursor ? { id: cursor } : undefined,
                orderBy: {
                    updatedAt: 'asc',
                },
            });
            let nextCursor: typeof cursor | undefined = undefined;
            if (items.length > limit) {
                const nextItem = items.pop();
                nextCursor = nextItem!.id;
            }
            return {
                items,
                nextCursor,
            };
        }),
infinitePosts: protectedProcedure
        .input(
            z.object({
                limit: z.number().min(1).max(100).nullish(),
                cursor: z.string().nullish(), // <-- "cursor" needs to exist, but can be any type
            })
        )
        .query(async ({ ctx, input }) => {
            const limit = input.limit ?? 20;
            const { cursor } = input;
            const items = await ctx.prisma.post.findMany({
                take: limit + 1, // get an extra item at the end which we'll use as next cursor
                cursor: cursor ? { id: cursor } : undefined,
                orderBy: {
                    updatedAt: 'asc',
                },
            });
            let nextCursor: typeof cursor | undefined = undefined;
            if (items.length > limit) {
                const nextItem = items.pop();
                nextCursor = nextItem!.id;
            }
            return {
                items,
                nextCursor,
            };
        }),

Then on my client:
api.post.infinitePosts
api.post.infinitePosts
, but there is no
.add
.add
or
.delete
.delete
on it, am I miss something or is there any detail doc?
useInfiniteQuery | tRPC
- Your procedure needs to accept a cursor input of any type (string, number, etc)
useInfiniteQuery | tRPC
tRPCJoin
Move Fast & Break Nothing. End-to-end typesafe APIs made easy.
5,015Members
Resources
Recent Announcements

Similar Threads

Was this page helpful?

Similar Threads

TRPC Stream not working,
2035Builder22035Builder / ❓-help
2y ago
TRPC not working on multi tenant app
JaaneekJJaaneek / ❓-help
3y ago
tRPC typings not working - Cannot import @trpc/server on client
Ofir SmolinskyOOfir Smolinsky / ❓-help
2y ago