wp-graphql-woocommerce icon indicating copy to clipboard operation
wp-graphql-woocommerce copied to clipboard

Product Query with Reverse Pagination and Orderby

Open MonPetitUd opened this issue 10 months ago • 0 comments

Describe the bug When querying products with "last", "before" and "orderby" inputs the returned products are not as expected.

To Reproduce Initial query to get some products and pageInfo...

query products {
	products(first: 5, where: { status: "PUBLISHED" , orderby: [ { field: NAME, order: ASC }]} ) {
		edges {
			cursor
			node {
				id 
				name
			}
		}
		pageInfo {
			startCursor
			endCursor
			hasNextPage
			hasPreviousPage
		}
	}
}

Using the "endCursor" from this query as the "after" input the following query we get the next 5 products...

query productsForward {
	products(first: 5, after: "YXJyYXljb25uZWN0aW9uOjc4Nw==", where: { status: "PUBLISHED" , orderby: [ { field: NAME, order: ASC }]} ) {
		edges {
			cursor
			node {
				id 
				name
			}
		}
		pageInfo {
			startCursor
			endCursor
			hasNextPage
			hasPreviousPage
		}
	}
}

Using the "startCursor" from the "productsForward" pageInfo result in the "before" input we try to go back... but we get unexpected resulting products in a strange order.

query productsBackward {
	products(last: 5, before: "YXJyYXljb25uZWN0aW9uOjMwMTE=", where: { status: "PUBLISHED" , orderby: [ { field: NAME, order: ASC }]} ) {
		edges {
			cursor
			node {
				id 
				name
			}
		}
		pageInfo {
			startCursor
			endCursor
			hasNextPage
			hasPreviousPage
		}
	}
}

Expected behavior Given the provided inputs "productsBackward" query should return the same products as the initial "products" query. If we omit the "orderby" input in all the above queries everything appears to work as expected.

Plugin Versions

  • WooGraphQL Version: 0.19.0
  • WPGraphQL Version: 1.22.1
  • WordPress Version: 6.4.3
  • WooCommerce Version: 8.6.1

Additional context I could be doing it wrong :)

MonPetitUd avatar Apr 02 '24 09:04 MonPetitUd