Prisma:在突变解析器中使用计数

Posted

技术标签:

【中文标题】Prisma:在突变解析器中使用计数【英文标题】:Prisma: use count inside a mutation resolver 【发布时间】:2019-06-13 13:22:36 【问题描述】:

我有一个突变createPet,如果用户已经创建了 5 个宠物,我希望它抛出一个错误。 我在想这样的事情会起作用,但它不会:

  const petsCount = await db.pets(
    where:  owner: user.id ,
  ).count();

我也没有在文档中找到任何内容

【问题讨论】:

【参考方案1】:

要检索计数,您可以使用聚合:

const petsCount = await prisma
  .petsConnection(where: owner: user.id)
  .aggregate()
  .count()

来源:documentation

【讨论】:

以上是关于Prisma:在突变解析器中使用计数的主要内容,如果未能解决你的问题,请参考以下文章