Certain OrderSortKeys causing strange pagination behavior

andrewbraun
Shopify Partner
2 0 0

Hello!

 

tl;dr: my Orders GraphQL query works just fine when sorting by CREATED_AT, but freaks out and won't return the requested number of orders per page when sorting by TOTAL_PRICE or ORDER_NUMBER, instead returning just a few or 0.

 

I have a Next.js app that fetches orders from the Admin API (version 2023-07) and displays them. I have a sorting function enabled and allow sorting by CREATED_AT, TOTAL_PRICE, and ORDER_NUMBER. I fetch the orders from the API using a GraphQL query (below) and this works just fine when I make a request for orders sorted by CREATED_AT, both in ascending (reverse: false) and descending (reverse: true) order. If I request 10 items/page, I get 10 items/page, all correctly sorted.

 

However, if I use TOTAL_PRICE, for example, changing nothing else about the query except the sortKey, the first page returned in reverse/descending order only returns two results, despite having dozens of orders to sort. When sorted in non-reverse/ascending order, I don't get any results at all. The pagination cursors work about half the time, and sometimes they'll bring me to a new page with different orders on it after clicking a few times, but usually not.

 

So, it seems like when sorting by TOTAL_PRICE, I'm somehow not getting the requested count of products per page. The same is true for ORDER_NUMBER, but only when I query with reverse: false. The exact same query works perfectly to sort orders by CREATED_AT, both in reverse and non-reverse order, so it seems that there's specifically an issue with the sortKey.

 

Weirdly, though, when I try the exact same query in the GraphiQL app, I get the correct results--10 per page every time. I've even copy-pasted that exact same query, with hard-coded variables, into my app and I get the same issue. I'm using the most recent version of the API (2023-07) and all the most up-to-date Shopify npm packages (@shopify/shopify-api 7.5.2, @Shopify/app 3.8.1).


Here's the main query I'm using, copy-pasted from my app (it's identical to the working query in the GraphiQL app).

query paginatedOrders($numItems: Int!, $cursor: String, $sortKey: OrderSortKeys, $reverse: Boolean) {
          orders(
            first: $numItems, 
            after: $cursor,
            sortKey: $sortKey,
            reverse: $reverse,
          ) {
            edges {
              node {
                id
                name
                createdAt
                currencyCode
                currentTotalPriceSet {
                  shopMoney {
                    amount
                    currencyCode
                  }
                }
                currentTotalTaxSet {
                  shopMoney {
                    amount
                    currencyCode
                  }
                }
                currentSubtotalLineItemsQuantity
                customer {
                  id
                  displayName
                }
              }
            }
            pageInfo {
              hasNextPage
              hasPreviousPage
              startCursor
              endCursor
            }
          }
        }

 

And here are the variables that would go into a TOTAL_PRICE query:

{
  "cursor": null,
  "numItems": 10,
  "sortKey": "TOTAL_PRICE",
  "reverse": false
}

 

This query works perfectly in GraphiQL, but in my app, it returns an empty edges array of orders. If I increase "first" to 50, I get 4 orders. 100 gets me 30 orders. 200 gets me 41, and so on. When I sort by CREATED_AT, this exact query gets me 10 orders per page. When I add in some dynamic variables for next/previous pagination, it works exactly as expected with CREATED_AT, but, again, not at all as expected for TOTAL_PRICE.

 

What could be the issue here? Am I being colossally unobservant and missing something obvious, or is this a bug nobody's talking about?

 

Happy to provide more details if requested!

Reply 1 (1)
andrewbraun
Shopify Partner
2 0 0

Still having this issue--I actually found this thread while Googling it again 😄 Can anyone help out here?