如何在GraphQL查询中使用distinct?

use*_*888 7 graphql

这是我尝试在graphQl查询中使用与众不同的查询:

query{
    contacts(take: 10, distinct: true) {
        firstName
        lastName
        title
    }
}
Run Code Online (Sandbox Code Playgroud)

但我得到了错误:

{
    "errors": [
        {
            "message": "Unknown argument \"distinct\" on field \"contacts\" of type \"QuerySchema\".",
            "locations": [
                {
                    "line": 2,
                    "column": 21
                }
            ]
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

Tom*_*Tom 8

GraphQL 没有内置的排序/过滤。由服务器来实现这样的功能,所以如果你依赖第三方 API 并且它不支持它,那么你将不得不自己过滤响应。


azn*_*nix -3

这是一个不同查询的示例。

query {
  contacts {
    distinct(field: title)
  }
}
Run Code Online (Sandbox Code Playgroud)

结果将会是。

{
  "data": {
    "contacts": {
      "distinct": [
        "This is my test post",
        "This is my test post1",
        "This is my test post2"
      ]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

此查询绑定所有标题并删除重复项。

  • 除非 API 的作者实现了该功能,否则这是行不通的。 (9认同)