How to sort query results with GraphQL and AWS AppSync

This post assumes you have a general understanding of GraphQL, AppSync, and DynamoDB.

Phil Andrews
The Startup
Published in
3 min readMay 4, 2020

--

The GraphQL backend used in AWS AppSync is most often connected to DynamoDB, a NoSQL database. If you’re here you know that a NoSQL database does not provide ordered results when you run a query.

Running an identical query multiple times against a DynamoDB table can produce different set, or order, of results in each response. We can solve that problem with a global secondary index and sort key.

Desired Outcome

Every time you run your new query it returns the objects in order of dueDate, most recent to least recent, from your table.

We’re using dueDate in this example but you can change that to whatever your schema requires.

Implementation

We need to create a new index (global secondary index) on the table that we want to query. AppSync has built-in functionality for this.

Our initial model looks like this:

--

--