How to use distinct in GraphQL query?

╄→гoц情女王★ 提交于 2021-01-19 19:30:32

问题


Here is query where I try to use distinct in graphQl query:

query{
    contacts(take: 10, distinct: true) {
        firstName
        lastName
        title
    }
}

But I am getting error:

{
    "errors": [
        {
            "message": "Unknown argument \"distinct\" on field \"contacts\" of type \"QuerySchema\".",
            "locations": [
                {
                    "line": 2,
                    "column": 21
                }
            ]
        }
    ]
}

回答1:


GraphQL has no built-in sorting/filtering. It is up to the server to implement features like that, so if you're relying on a third party API and it doesn't support it then you will have to filter the response yourself.




回答2:


Here is a example of distinct query.

query {
  contacts {
    distinct(field: title)
  }
}

Result will be.

{
  "data": {
    "contacts": {
      "distinct": [
        "This is my test post",
        "This is my test post1",
        "This is my test post2"
      ]
    }
  }
}

This query binds all titles and deduplicates.



来源:https://stackoverflow.com/questions/46552167/how-to-use-distinct-in-graphql-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!