Order By in GraphQL

Open inAnthropic

What you'll achieve By the end of this guide, you will know how to sort and order 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"}'

Ordering results of a GraphQL query

By default, results in GraphQL will choose an implementation specific order which may not even be stable between invocations. If you need results in a specific order then you need to supply an orderBy argument.

We can search for the names of people in reverse alphabetical order such that we only recover the first 5 results in the following way:

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

This will give us the following people:

Example: JSON
{
  "data": {
    "People": [
      {
        "label": "Zam Wesell"
      },
      {
        "label": "Yoda"
      },
      {
        "label": "Yarael Poof"
      },
      {
        "label": "Wilhuff Tarkin"
      },
      {
        "label": "Wicket Systri Warrick"
      }
    ]
  }
}

Order by can also take more than one argument, allowing us to order on more than one value using the remaining arguments when there is a tie in the preceding (lexicographic ordering).

We can see this by searching for species, and which language they speak and their name. Since many will share the same language, we can see the ordering of the fields independently.

Example: GraphQL
query{
   Species(offset:8, limit:5, orderBy:{language:ASC, label:ASC}){
    label
    language
  }
}

And here we have a number of Galactic Basic speakers who nevertheless are ordered by species name.

Example: JSON
{
  "data": {
    "Species": [
      {
        "label": "Ewok",
        "language": "Ewokese"
      },
      {
        "label": "Human",
        "language": "Galactic Basic"
      },
      {
        "label": "Rodian",
        "language": "Galactic Basic"
      },
      {
        "label": "Yoda's species",
        "language": "Galactic basic"
      },
      {
        "label": "Geonosian",
        "language": "Geonosian"
      }
    ]
  }
}

Was this helpful?