How to get total edge count with prismaNode
and prismaConnection
?
#343
-
I have something like this builder.prismaNode('Product', {
include: { _count: true },
findUnique: id => ({ id }),
id: { resolve: product => product.id },
fields: t => ({
createdAt: t.expose('createdAt', { type: 'DateTime' }),
massKg: t.expose('massKg', { type: 'Decimal', nullable: true }),
tolerance: t.expose('tolerance', { type: 'Decimal', nullable: true }),
name: t.exposeString('name'),
description: t.exposeString('description', { nullable: true }),
location: t.exposeString('location', { nullable: true }),
}),
})
builder.queryField('products', t =>
t.prismaConnection({
type: 'Product',
cursor: 'id',
resolve: (query, parent, args, { prisma }) =>
prisma.product.findMany({ ...query }),
}),
) I'm currently using this without issue, but I need somehow to get the total count in order to calculate the total pages in my pagination bar. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
If you don't need to arguments for the field, this is pretty easy: builder.queryFields((t) => ({
posts: t.prismaConnection(
{
type: 'Post',
cursor: 'id',
resolve: (query) =>
db.post.findMany({
orderBy: { createdAt: 'desc' },
...query,
}),
},
{
fields: (t) => ({
totalCount: t.int({
resolve: () => db.post.count(),
}),
}),
},
),
})); Unfortunately the arguments for the prismaConnection field are not directly available in fields defined on the connection object. If you need to access arguments to get the count, this won't work. I can't think of a simple solution currently, and that might require some changes to the plugin to make that work. If you have any suggestions on how you would want this to work let me know! |
Beta Was this translation helpful? Give feedback.
If you don't need to arguments for the field, this is pretty easy:
Unfortunately the arguments for the prismaConnection field are not directly available in fields defined on the connection object. If you need to access arguments to get the count, this won't work. I can't think of a simple solution currently, and that …