首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Connection从prisma1到prisma2进行分页

如何使用Connection从prisma1到prisma2进行分页
EN

Stack Overflow用户
提问于 2020-08-19 23:52:48
回答 2查看 258关注 0票数 0

我一直在使用prisma1中的这个模式使用usersConnection来管理分页:

代码语言:javascript
复制
"""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吗?有没有更好的方法?

EN

回答 2

Stack Overflow用户

发布于 2020-08-20 00:30:55

解决方案是:

代码语言:javascript
复制
    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 })
      }
    }
票数 0
EN

Stack Overflow用户

发布于 2020-09-29 20:27:01

最新的@prisma/client具有带有count方法的聚合API (请参阅https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client/aggregations)

下面是一个nexus示例:

代码语言:javascript
复制
t.field('usersCount', {
  type: 'Int',
  args: {
    where: 'UserWhereInput',
  },
  resolve: (parent, { where }, { prisma }) => {
    return prisma.user.count({ where }) // <--- the solution
  },
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63490734

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档