wp-graphql / wp-graphql-woocommerce

Add WooCommerce support and functionality to your WPGraphQL server
https://woographql.com
GNU General Public License v3.0
633 stars 123 forks source link

Product Query with Reverse Pagination and Orderby #843

Open MonPetitUd opened 3 months ago

MonPetitUd commented 3 months ago

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

Additional context I could be doing it wrong :)