CodexBloom - Programming Q&A Platform

AWS AppSync and DynamoDB Pagination Handling guide with VTL Resolvers

👀 Views: 0 💬 Answers: 1 📅 Created: 2025-06-15
AWS AppSync DynamoDB VTL pagination GraphQL

I'm following best practices but This might be a silly question, but I'm currently working on an AWS AppSync project where I'm trying to implement pagination for a DynamoDB data source using VTL (Velocity Template Language) resolvers... Despite following the official documentation, my implementation doesn't appear to be returning the expected results when I try to paginate through my data. I have a query that retrieves items from a DynamoDB table, but when I add pagination parameters, it seems like the `nextToken` is not being recognized. Here's the basic setup of my GraphQL schema: ```graphql type Query { listItems(limit: Int, nextToken: String): ItemConnection } type ItemConnection { items: [Item] nextToken: String } type Item { id: ID! name: String! } ``` And in my VTL request mapping template for the `listItems` resolver, I'm using: ```vtl #set($limit = $ctx.args.limit) #set($nextToken = $ctx.args.nextToken) { "version": "2018-05-29", "operation": "Scan", "limit": $limit, "exclusiveStartKey": $nextToken } ``` However, when I test the query like this: ```graphql query ListItems { listItems(limit: 5) { items { id name } nextToken } } ``` The result returns the first batch of items correctly, but when I try to fetch the next set of items, it continues to return the same batch despite providing a `nextToken` in the subsequent query. I have also checked the DynamoDB console, and it seems like the `nextToken` is being generated correctly, but I'm not sure if I'm using it correctly in the VTL resolver. I’ve tried changing the `operation` from `Scan` to `Query` as well, but that didn’t fix the scenario either. The logs are not showing any errors, and I’m using the latest version of AWS AppSync. Has anyone experienced a similar scenario with pagination in AppSync using VTL? What am I missing in the implementation? What's the best practice here? I'm working on a web app that needs to handle this. Could someone point me to the right documentation?