AppSync/GraphQL : Pagination on DynamoDB data: Curser based pagination

What is AppSync?

AWS AppSync helps you create a flexible API to securely access, manipulate, and combine data from one or more data sources.AppSync uses GraphQL. AppSync can be used along with Amazon services like Lambda, DynamoDB, etc..

What is GraphQL?

GraphQL is a language for APIs that enables you to query and manipulate data easily through an intuitive and flexible syntax. GraphQL provides a syntax to describe data requirements and interactions, allowing you to ask for exactly what you need and get back predictable results.

This article aims at providing strategy on how to query data in the DynamoDB table with sorting and pagination. The pagination strategy used is called cursor-based pagination. Curser based pagination helps to avoid the problems of limit-offset pagination. Shifting of data on insertion and on deletion occurs while paginating in a limit-offset way, this is not ideal for real-time data.

Whats is curser based pagination?

Cursor-based pagination works using a pointer to a specific item in the dataset. By using this strategy major pitfalls of limit-offset pagination can be avoided.



Achieving pagination on DynamoDB data with AppSync/GraphQL

Consider a case where we have an Entity Employees and each employee has multiple  Tasks. It’s a one-to-many relationship.

step1: Create an API and add schema – To create the schema for such an API, Go to  services->AppSync and click on create API. Choose Build from scratch.  After creation goto edit schema and add schema.

Schemas are written using Schema Definition Language(SDL) .


What is SDL(Schema definition language)?

It’s a language with a very simple syntax that allows to define a schema


Schema expects query. Subscribtion and Mutation can be also provided to subscribe to and mutate DynamoDB data source. Data Source should be attached to the schema using the resolvers.

step2: Create tables , add data sources(DynamoDB), add resolvers  – Goto Data Sources and add the DynamoDB tables for the API. If tables are not created, create them. Add resolvers, in this case, we can add resolvers for the get query. Resolvers for the query for getting tasks, can be added after creating it.


What are data sources  for APIS Appsync?

AWS AppSync enables you to use supported data sources (that is, AWS Lambda, Amazon DynamoDB, or Amazon Elasticsearch Service) to perform various operations, in addition to any arbitrary HTTP endpoints to resolve GraphQL fields. After your HTTP endpoints are available, you can connect to them using a data source.

  What are resolvers?

      Resolvers are the ones that translate GraphQL requests or responses to Data sources.



In this case, create two tables employee and tasks. The table tasks have fields employeeId  and  createdDate, the combination of both will be a primary key and we create a secondary index(GSI) using them. employeeId is the partition key and createdDate is the sort key. Dynamodb stores data in partitions and they will be in the order of sort key,  if sort key exists. The combination of both keys forms primary key. Primary key is unique.

Note that if you have the sort key, only then you can get the paginated data in order.



step3: Creating a type, Connection in the schema and adding query for pagination, into the schema 

A connection is a way to get all of the nodes that are connected to another node in a specific way

In this case, we want to get all of the task nodes connected to our employees. Connection of an employee to multiple tasks can be defined in the following way in Graph.

The TaskConnection type has an array of tasks. limit will have the number of items needed and nextToken will have the cursor to the task from which the data is required.

Now create a query getAllTaskConnection in schema, that will get the tasks corresponding to an employee. Add resolvers of the query to the tasks table.

Now, add resolver for getAllTaskConnection,  with the DynamoBD table tasks as the data source. Add the following request and response mapping template.


scanIndexForward:true – If ScanIndexForward is true, DynamoDB returns the results in the order in which they are stored. false means in the reverse order


Now add some dummy data into the tasks table.


Query to get data will look like this.



Get the next two with the next token


So this is it. nextToken will be the cursor from where the next data should be taken.

This is the way to achieve Curser-based pagination in Appsync.

Post your suggestions and corrections in the comment box below

No Comments

Leave a Reply

Your email address will not be published. Required fields are marked *