How should I extract largest value or latest timestamp data in a graphQL query

Viewed 184

When I execute following graphQL query which has only one function and I get output which is shown below.

I want output which has largest ID or the latest timestamp.

It is possible by making change in API but my constraint is not to make any change in API and have enhance the query only, Please help me how can I achieve my goal/ desired output

Input

query getAllCriticalevent{
    getAllCriticalevent(patientId: 95)
  {
    id
    startTime
  }
}

Output

{
  "data": {
    "getAllCriticalevent": [
      {
        "id": "107",
        "startTime": "2019-06-14 12:47:57.0"
      },
      {
        "id": "1464",
        "startTime": "2019-10-10 16:08:35.0"
      },
      {
        "id": "1465",
        "startTime": "2019-10-10 16:09:09.0"
      },
      {
        "id": "1466",
        "startTime": "2019-10-10 16:09:44.0"
      },
      {
        "id": "1469",
        "startTime": "2019-10-10 16:11:28.0"
      },
      {
        "id": "1470",
        "startTime": "2019-10-10 16:12:03.0"
      },
      {
        "id": "1484",
        "startTime": "2019-10-10 16:20:09.0"
      }
    ]
  }
}

My expected output is this

{
    "startTime": "2019-10-10 16:20:09.0"
       }

or

{
        "id": "1484",
        "startTime": "2019-10-10 16:20:09.0"
      }
1 Answers

One way to do this is to add a column to the Type definition, then return it from your resolver.

In Laravel (not Java), the definition:

'max' => [
    'type' => Type::int(),
    'description' => 'The highest score achieved'
],

and a separate query in the ORM resolver (getMaxAttribute() is referenced as simply .max()):

public function getMaxAttribute() {
    return DB::table('players')->max('score');
}

will return the max for a desired column. You request the column by name in GraphQL, just like normal (eg. "{ ... max }").

Related