Infinite Query crashes Next.js router

so long story short, when I use the infinite query, cursor based pagination version of the query here, my next.js router crashes. like flat out dies. I have to kill the process manually via PID, it's wild. If i use the normal non cursor paginated version, then it works fine. I'd like to be able to use cursor based pagination, what makes matters more confusing, is it works totally fine with another infinite query, but with a different prisma object, even the same code structure for the query itself. I'm genuinely at a loss for what could be causing this. I'm starting to think it's something in prisma? Queries to follow as I don't have nitro
1 Reply
Bryan
BryanOP10mo ago
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
}),
Want results from more Discord servers?
Add your server