question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AppSync: Cursor based pagination has a funny bug where NO items are returned, but a nextToken is.

See original GitHub issue

Describe 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:closed
  • Created 3 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
hutchidacommented, Oct 26, 2020

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.

0reactions
github-actions[bot]commented, Dec 12, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

AppSync pagination issue where no items are returned but a ...
This does not work correctly, when I first query for the todos, it returns an empty items array + a NON NULL nextToken...I...
Read more >
Implementing pagination with AWS AppSync
The query results may return a different nextToken . This is because items may have been inserted or deleted since the nextToken A...
Read more >
A problem with pagination AWS Amplify API
I am writing an application based on Amazon AWS amplify. ... The server returns to me only 10 notes and nextToken for receiving...
Read more >
“Guys, we're doing pagination wrong…” | HackerNoon
Unidirectional paging with cursor​​ For unidirectional pagination, my preferred approach is to use a simple cursor. The important detail here is ...
Read more >
Untitled
Swiss army cards victorinox, Herbs parts middletown de, Emmerdale village eccup, 30 days return, Oriental estereo 90.6, #Pat and jen ep 40, Ascii...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found