6

I have been working on a GraphQL and firing queries to get the JSON response into react app.

But I need to fire a query which will return me the max and min value from the column.

I have gone through the document of GraphQL but nowhere I found the way to accomplish this.

{
  schneider(where: {_and: [{device_time: {_gte: "2018-09-04T20:11:42.097+05:30"}}, {device_time: {_lte: "2018-09-04T20:12:43.187+05:30"}}]}) {
    active_energy_received
  }
}

The above query gives me the list of values that are between the two provided dates.

{
  "data": {
    "schneider": [
      {
        "active_energy_received": 0
      },
      {
        "active_energy_received": 0
      },
      {
        "active_energy_received": 0
      },
      {
        "active_energy_received": 0
      },
      {
        "active_energy_received": 0
      },
      {
        "active_energy_received": 0
      },
      {
        "active_energy_received": 0
      },
      {
        "active_energy_received": 0
      },
      {
        "active_energy_received": 0
      },
      {
        "active_energy_received": 0
      },
      {
        "active_energy_received": 4.3699998856
      },
      {
        "active_energy_received": 0.82099998
      },
      {
        "active_energy_received": 0.82099998
      },
      {
        "active_energy_received": 4.3699998856
      },
      {
        "active_energy_received": 0.82099998
      },
      {
        "active_energy_received": 4.3699998856
      },
      {
        "active_energy_received": 0.82099998
      },
      {
        "active_energy_received": 4.3699998856
      },
      {
        "active_energy_received": 0.82099998
      },
      {
        "active_energy_received": 4.3699998856
      },
      {
        "active_energy_received": 0.82099998
      },
      {
        "active_energy_received": 4.3699998856
      },
      {
        "active_energy_received": 0
      },
      {
        "active_energy_received": 0.820999979972839
      },
      {
        "active_energy_received": 4.3699998856
      }
    ]
  }
}

Now, instead of all the values, I want only max and min value from this list.

UPDATE:

I am using hasura.io with postgreSQL.

Vishal Shetty
  • 1,618
  • 1
  • 27
  • 40
  • Maybe you can define a custom parameter for this special use case? Something like `schneider(where: ..., onlyFirstAndLast: true)`. Which will then use the results of the `where` and execute some custom logic that will only return index 0 and index n-1. Don't know if that's possible with hasura – Benjamin M Feb 03 '19 at 00:20

2 Answers2

8

If you are using Hasura.io then you can use the customized API queries to ORDER BY and LIMIT for the MIN and MAX result.

Example for MAX value:

query {
  table_name(limit: 1, order_by: {time: desc}) { 
      data 
      time
    }
}

Example for MIN value:

query {
  table_name(limit: 1, order_by: {time: asc}) { 
      data 
      time
    }
}
1

GraphQL doesn't have a native way to do this. (If SQL is your base, there are a lot of things like ORDER BY, GROUP BY, and anything other than simple column selects that GraphQL doesn't provide natively.)

If the server is fixed but you're getting that query response, you can calculate the aggregate yourself on the client side.

If you control the server too, and you find yourself needing aggregates like this, you can add them to the schema. Given your example query I might add a layer of indirection in between the query and its results.

type Query {
    schneider(where: SchneiderQuery): SchneiderPayload!
}
type SchneiderPayload {
    results: [SchneiderResult!]!
    max: SchneiderResult
    min: SchneiderResult
}

With that schema you could write a query like

{
    schneider(where: { ... }) {
        max { active_energy_received }
    }
}

In your server code you'd have to have a special resolver for the max field that generated the correct query.

David Maze
  • 130,717
  • 29
  • 175
  • 215
  • (I'm a little dissatisfied by this approach, mostly in that if the response records have links to other object types, the artificial "min" and "max" records can't have those links, or `id` fields. Still more restructuring could get around this.) – David Maze Oct 08 '18 at 12:30
  • sorry, I didn't update the other information in my question, I am using hasura.io and database is postgreSQL, I am not sure if I can define the schema as I am getting everything readymade from hasura. – Vishal Shetty Oct 08 '18 at 12:31
  • for something that claims to be so powerful(and in many ways, is), it is completely unacceptable for such basic functionality not to exist. The CEO of Apollo graphQL(and creator of Meteor) gave a presentation at a coding competition I was taking part in last weekend and I wish I knew this then because I would have brought it up. – MattE Jun 26 '19 at 13:55