Skip to content

Pagination - previous products, last and before #928

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Nico2d opened this issue Mar 20, 2025 · 1 comment
Open

Pagination - previous products, last and before #928

Nico2d opened this issue Mar 20, 2025 · 1 comment

Comments

@Nico2d
Copy link

Nico2d commented Mar 20, 2025

Describe the bug
I am experiencing an issue with back pagination, after using last and before props with startCursor of the "nextPage" I can't go back to previous products.

To Reproduce
My query:

query AllProducts($filters: [ProductTaxonomyFilterInput], $minPrice: Float, $maxPrice: Float, $after: String, $first: Int, $last: Int, $before: String) {
  products(
    first: $first
    after: $after
    last: $last
    before: $before
    where: {minPrice: $minPrice, maxPrice: $maxPrice, taxonomyFilter: {filters: $filters} orderby: { field: NAME order: DESC}}
  ) {
    pageInfo {
      endCursor
      hasNextPage
      startCursor
      hasPreviousPage
    }
    nodes {
      databaseId
      name
      slug
    }
  }
}

Products on the 1 page:
Image

Products on the 2 page:
(using the endCursor tag on after property)
Image

Try to go back to 1 page:
(using the startCursor tag on before property and change using first to last)

Image
As you can see, there is different products then on page 1, and we didn't get hasPreviousPage: false

Expected behavior
After using before we should go back to previous page

Plugin Versions

  • WooGraphQL Version: - 0.21.2
  • WPGraphQL Version: - 1.29.3
  • WordPress Version: - 6.7.2.
  • WooCommerce Version: - 9.4.2
@kidunot89
Copy link
Collaborator

kidunot89 commented Apr 17, 2025

@Nico2d Can you try changing your orderby input to { field: NAME } or { field: NAME, order: ASC}. Iirc when you use the before and last the default sort is actually reversed by the WPGraphQL core's AbstractConnectionResolver class' logic. In you're last example the startCursor corresponds with the product with ID 806 when the result list is reversed by last and before, with your orderby.order: DESC input applied to the query, it get last ten that came before it alphabetic which in this case which is product with IDs 805-661, and not 884-806.

What I'm trying to say with all this is the sort in the orderby is throwing off your expected sort, just remove the order from you orderby input and use first and last to control you sort order if you plan on paginating the list in both directions otherwise when you switch to last and before you have to also invert the orderby.order as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants