简体   繁体   English

有没有办法在 GraphQL 中执行 GROUP BY 操作?

[英]Is there any way to perform GROUP BY operation in GraphQL?

I need to group data in a table by using a particular field in the table.我需要使用表中的特定字段对表中的数据进行分组。 And couldn't find GROUP BY feature in GraphQL.并且在 GraphQL 中找不到 GROUP BY 功能。 Is there any way to achieve that?有没有办法做到这一点?

Current Format:当前格式:

[
   {
      "my_state":"karnatak",
      "from_state":"kerala",
      "route":"wayanad"
   },
   {
      "my_state":"karnatak",
      "from_state":"kerala",
      "route":"Mangalore"
   },
   {
      "my_state":"karnatak",
      "from_state":"kerala",
      "route":"Palakkad"
   },
   {
      "my_state":"karnatak",
      "from_state":"tamilnadu",
      "route":"hosure"
   },
   {
      "my_state":"karnatak",
      "from_state":"tamilnadu",
      "route":"selam"
   },
   {
      "my_state":"karnatak",
      "from_state":"tamilnadu",
      "route":"chennai"
   }
]

Required Format:所需格式:

from_state: "karnatak",
  route: [
    {
      route: "chennai"
    },
    {
      route: "selam"
    },
    ............
  ]    

GraphQL is a query language for APIs and it does not really "contain features" like GroupBy, Count, Sort, etc. Those features or queries have to be implemented in the server (GraphQL API) and it leaves you a couple of options: GraphQL 是一种API 查询语言,它并不真正“包含特性”,如 GroupBy、Count、Sort 等。这些特性或查询必须在服务器中实现(GraphQL API),它为您提供了几个选项:

  • If you own the API(server) then you would have to create your own "GroupBy query" that would allow you to fetch the data the way you would like.如果您拥有 API(服务器),那么您将必须创建自己的“GroupBy 查询”,以允许您以您想要的方式获取数据。
  • If you are just using the API you can use the schema documentation to search if there is a query format that satisfies your need.如果您只是使用 API,您可以使用架构文档来搜索是否有满足您需要的查询格式。
  • You can also make your own groupBy function after fetching the data, there's some good examples in these answers .您也可以在获取数据后通过 function 创建自己的 group ,这些答案中有一些很好的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM