DEV Community

adegiamb-dev
adegiamb-dev

Posted on

Offset pagination and total count?

Overview

I am playing with Hasura / grapghql and having an issue with offset pagination to include total number of records in certain structure.

Query

Below is an example query I am doing to get the second page with 3 items.

query getItems {
  items(offset: 2, limit: 3) {
    id
  }
  items_aggregate {
    aggregate {
      count
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Query Results

You can see that to get the total number of records the property is item_aggregated.aggregate.count. That is a bit messy and long just for a total count.

{
  "data": {
    "items": [
      {
        "id": "829b1baa-344d-4360-8d11-eb31fa592f29"
      },
      {
        "id": "0ac64c11-1e40-44c3-84ad-4eb5e2e495a9"
      },
      {
        "id": "827fcfa8-6ce6-4612-9bab-3ea62969f041"
      }
    ],
    "items_aggregate": {
      "aggregate": {
        "count": 100
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Required Results

Below is the result structure I would like to get back. You can see that items now have 2 different properties:

  1. records array of the actual data
  2. total an integer for the total record count.
{
  "data": {
    "items": {
       "records":[
            {
              "id": "829b1baa-344d-4360-8d11-eb31fa592f29"
            },
            {
              "id": "0ac64c11-1e40-44c3-84ad-4eb5e2e495a9"
            },
            {
              "id": "827fcfa8-6ce6-4612-9bab-3ea62969f041"
            }
          ],
       "total":100
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Question

To my understanding this can be done by using a resolver or a custom function but not sure how you do this. If someone can provide me sample on how to do this?

Top comments (2)

Collapse
 
akashkaintura profile image
AKASH KAINTURA

Is it similar to BSON?

Collapse
 
adegiambdev profile image
adegiamb-dev

I don't think so. I just trying to determine how to support a total count and have a clean data structure.