我一直在使用prisma1中的这个模式使用usersConnection来管理分页:
"""A connection to a list of items."""
type UserConnection {
"""Information to aid in pagination."""
pageInfo: PageInfo!
"""A list of edges."""
edges: [UserEdge]!
aggregate: AggregateUser!
}
"""An edge in a connection."""
type UserEdge {
"""The item at the end of the edge."""
node: User!
"""A cursor for use in pagination."""
cursor: String!
}
type AggregateUser {
count: Int!
}现在,是时候迁移到prisma2了。我必须保留这种格式,因为不同的前端都在使用这种模式,有什么建议可以用prisma2重新创建这种分页吗?我们应该解析并重新创建对象users吗?有没有更好的方法?
发布于 2020-08-20 00:30:55
解决方案是:
const users = await ctx.prisma.user.findMany({
where: args.where,
skip: args.skip,
take: args.first,
})
return {
edges: users.map((singleData: Source) => { return { node: singleData } }),
aggregate: {
count: await ctx.prisma.user.count({ where: args.where })
}
}发布于 2020-09-29 20:27:01
最新的@prisma/client具有带有count方法的聚合API (请参阅https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client/aggregations)
下面是一个nexus示例:
t.field('usersCount', {
type: 'Int',
args: {
where: 'UserWhereInput',
},
resolve: (parent, { where }, { prisma }) => {
return prisma.user.count({ where }) // <--- the solution
},
})https://stackoverflow.com/questions/63490734
复制相似问题