Strapi GraphQL中的嵌套查询

时间:2019-09-30 05:38:57

标签: graphql strapi

我的文档结构大致如下:

post {
  _id
  title
  isPublished
}

user {
  _id
  username
  name
  [posts]
}

我知道我可以使用postConnection子字段查询userConnectionaggregate之类的字段,以便查询所有对象的数量。但是如何获取给定posts的所有user的总数?

我能够想到这个:

{
  postsConnection(where: {isPublished: true}){
    groupBy{
      author{
        key
        connection{
          aggregate{
            count
          }
        }
      }
    }
  }
}

但这会返回(预期)这样的内容:

{
  "data": {
    "postsConnection": {
      "groupBy": {
        "author": [
          {
            "key": "5c9136976238de2cc029b5d3",
            "connection": {
              "aggregate": {
                "count": 5
              }
            }
          },
          {
            "key": "5c99d5d5fcf70010b75c07d5",
            "connection": {
              "aggregate": {
                "count": 3
              }
            }
          }
        ]
      }
    }
  }
}

如您所见,它返回数组中所有作者的帖子数。我需要的是能够只返回一个特定 user的计数,而不是返回_id的计数(key字段似乎映射到该计数) ),但我在users集合中拥有另一个唯一字段,即username

有可能吗?

1 个答案:

答案 0 :(得分:0)

需要将参数传递给查询或字段以返回特定数据

相关问题