2

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. 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"
    },
    ............
  ]    
Marco Daniel
  • 5,467
  • 5
  • 28
  • 36

1 Answers1

2

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:

  • 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.
  • 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.
  • You can also make your own groupBy function after fetching the data, there's some good examples in these answers.
Marco Daniel
  • 5,467
  • 5
  • 28
  • 36