Bryan
Bryan
TTCTheo's Typesafe Cult
Created by Bryan on 3/1/2024 in #questions
Infinite Query crashes Next.js router
Failing Query
myPlaylists: authProcedure
.input(
z.object({
limit: z.number().min(1).max(100).nullish(),
cursor: z.number().nullish(), // <-- cursor needs to exist but can be undefined
}),
)
.query(async ({ ctx, input }) => {
const limit = input.limit ?? 25
const { cursor } = input

console.log(input, ctx)

const playlists = await ctx.prisma.playlist.findMany({
take: limit + 1,
where: {
userId: ctx.user?.id,
},
cursor: cursor ? { id: cursor } : undefined, // 'id' should be the field you want to paginate by
orderBy: {
position: "asc",
},
select: {
id: true,
title: true,
position: true,
description: true,
// _count: {
// select: { playlistItems: true },
// },
},
})

let nextCursor: typeof cursor | undefined = undefined

if (playlists.length > limit) {
const nextItem = playlists.pop()
nextCursor = nextItem!.id
}

return { playlists, nextCursor }
}),
myPlaylists: authProcedure
.input(
z.object({
limit: z.number().min(1).max(100).nullish(),
cursor: z.number().nullish(), // <-- cursor needs to exist but can be undefined
}),
)
.query(async ({ ctx, input }) => {
const limit = input.limit ?? 25
const { cursor } = input

console.log(input, ctx)

const playlists = await ctx.prisma.playlist.findMany({
take: limit + 1,
where: {
userId: ctx.user?.id,
},
cursor: cursor ? { id: cursor } : undefined, // 'id' should be the field you want to paginate by
orderBy: {
position: "asc",
},
select: {
id: true,
title: true,
position: true,
description: true,
// _count: {
// select: { playlistItems: true },
// },
},
})

let nextCursor: typeof cursor | undefined = undefined

if (playlists.length > limit) {
const nextItem = playlists.pop()
nextCursor = nextItem!.id
}

return { playlists, nextCursor }
}),
This one is fine:
myPlaylists: authProcedure.query(async ({ ctx }) => {
const playlists = await ctx.prisma.playlist.findMany({
take: 25,
where: {
userId: ctx.user?.id,
},
orderBy: {
position: "asc",
},
select: {
id: true,
title: true,
position: true,
},
})
return playlists
}),
myPlaylists: authProcedure.query(async ({ ctx }) => {
const playlists = await ctx.prisma.playlist.findMany({
take: 25,
where: {
userId: ctx.user?.id,
},
orderBy: {
position: "asc",
},
select: {
id: true,
title: true,
position: true,
},
})
return playlists
}),
2 replies