AppSync: Cursor based pagination has a funny bug where NO items are returned, but a nextToken is.
See original GitHub issueDescribe the bug
When using cursor based pagination on an AppSync model list, it will return an empty items
array with a NON NULLnextToken
which signifies that there are more results, when I query for the next result set, it returns the 3 results that match my query and filter. I am using a limit of 10 & there are only 3 results, I’m confused to why AppSync is not returning these 3 results in my first query? (it should return the 3 results & the nextToken should be NULL)
Context:
- I have a tabview for a screen, the first tab shows the PENDING TODO items, the second tab shows the COMPLETED TODO items.
- I have 3 results that match my COMPLETED filter
- I have around 36 results that match my PENDING filter
Code Snippet / Explanations
This is the query for listing the PENDING todos, as u can see there is a limit of 10 & an optional nextToken. This work as expected, I am able to paginate the results correctly.
query listTodos($nextToken: String) {
listTodos(limit: 10, nextToken: $nextToken, filter: {
status: {
eq: PENDING
}
}) {
nextToken,
items {
id
title
status
}
}
}
This is the query for listing the COMPLETED todos, as u can see there is a limit of 10 & an optional nextToken.This does not work correctly, when I first query for the todos, it returns an empty items array + a NON NULL nextToken…I then have to run another query with the given nextToken to retrieve the 3 COMPLETED todos…this must be a bug?
query listTodos($nextToken: String) {
listTodos(limit: 10, nextToken: $nextToken, filter: {
status: {
eq: COMPLETED
}
}) {
nextToken,
items {
id
title
status
}
}
}
Expected behavior
When running my listTodos query for COMPLETED todos, it should return the 3 todos in the first query, I shouldn’t need to run a subsequent query with the returned nextToken to retrieve the ONLY 3 COMPLETED todos.
When doing pagination with a filter, it should return the ONLY 3 results matching the filter and return a null nextToken.
Additional context
- Is there any other context you guys need to help debug? Let me know.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (1 by maintainers)
Top GitHub Comments
Thanks @undefobj , also, for those who come across this thread, this article helped me understand what was going on: https://www.alexdebrie.com/posts/dynamodb-filter-expressions/ My solution was to set up new resolvers by modifying the appsync schemas. I would recommend reading this page: https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-dynamodb-resolvers.html Starting at ‘Setting Up the allPost Resolver (DynamoDB Scan)’ and getting all items out first, then moving on to the next section where you just want to get some items out based on a condition…i.e. a filter of sorts. In that document it’s the next resolver they create to get items by author name ‘Setting Up the allPostsByAuthor Resolver (DynamoDB Query)’. You can swap ‘author’ for anything else you may have in your table, but you must make sure the field you choose is set up as a GlobalSecondaryIndex. You can do this quite easily in the dynamodb console (https://us-west-2.console.aws.amazon.com/dynamodb/home?region=us-west-2#tables:), choose Tables > your table name, then in the right hand panel > Indexes > Create Index. Should be fairly self explanatory from there.
This issue has been automatically locked since there hasn’t been any recent activity after it was closed. Please open a new issue for related bugs.
Looking for a help forum? We recommend joining the Amplify Community Discord server
*-help
channels or Discussions for those types of questions.