【问题标题】:Nested query in Strapi GraphQLStrapi GraphQL 中的嵌套查询
【发布时间】:2020-01-29 09:38:04
【问题描述】:

我的文档结构大致如下:

post {
  _id
  title
  isPublished
}

user {
  _id
  username
  name
  [posts]
}

我知道我可以使用 aggregate 子字段查询像 postConnection 和 userConnection 这样的字段,以便查询所有对象的计数。但是如何通过给定的user 获得所有posts 的总数?

我想出了这个:

{
  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。

这可能吗?

【问题讨论】:

    标签: graphql strapi


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-29
      • 2020-12-02
      • 1970-01-01
      • 2018-06-13
      • 2019-05-30
      • 2021-01-14
      • 2017-06-16
      • 2017-07-17
      相关资源
      最近更新 更多