Limit Results with GraphQL

Open inAnthropic

What you'll achieve By the end of this guide, you will know how to limit and paginate results in GraphQL queries.

Prerequisites: TerminusDB running on localhost:6363 with the Star Wars dataset cloned. If you haven't done this yet, follow the Explore a Real Dataset tutorial (Steps 1–2), or run:

curl -u admin:root -X POST http://localhost:6363/api/clone/admin/star-wars \
  -H "Content-Type: application/json" \
  -H "Authorization-Remote: Basic cHVibGljOnB1YmxpYw==" \
  -d '{"remote_url": "https://data.terminusdb.org/public/star-wars", "label": "Star Wars", "comment": "Star Wars dataset"}'

Once you have cloned the database, go to the GraphQL icon (triangle in hexagon) on the left hand side and select the filing cabinet icon.

GraphQL query playground in TerminusDB

There are two panels, one on the left for query, and one on the right for results.

Adding a limit

The limit keyword is an argument which can be passed to a query to restrict the number of results to precisely the number supplied by the argument.

For instance we can get exactly 5 people from the Star Wars universe by specifying the query here:

Example: GraphQL
query{
   People(limit: 5){
      label
   }
}

This will result in

Example: JSON
{
  "data": {
    "People": [
      {
        "label": "Luke Skywalker"
      },
      {
        "label": "Obi-Wan Kenobi"
      },
      {
        "label": "Anakin Skywalker"
      },
      {
        "label": "Wilhuff Tarkin"
      },
      {
        "label": "Chewbacca"
      }
    ]
  }
}

If you want to page, to get the next results, you can use an offset

Was this helpful?