How to get customer's orders and sort by date in descending using storefront API?

Highlighted
New Member
3 0 1

Hi, I want to get the latest orders of a particular customer using storefront api. However, when I try to get the last 10 orders of a customer, it show an error "using last without before is not supported". I also tried to use the sortKey field but it seems only support sorting in ascending order. So, what should I do in order to simply get the last n orders of a customer?

 

query {
  customer(customerAccessToken: "xxxxx") {
    orders(last: 10) {
      edges {
        node {
          id
          name
          shippingAddress {
            formatted
          }
          lineItems(first: 250) {
            edges {
              node {
                title
                quantity
                discountAllocations {
                  allocatedAmount {
                    currencyCode
                    amount
                  }
                }
                variant {
                  image {
                    originalSrc
                  }
                  priceV2 {
                    currencyCode
                    amount
                  }
                  compareAtPriceV2 {
                    currencyCode
                    amount
                  }
                }
              }
            }
          }
          subtotalPriceV2 {
            currencyCode
            amount
          }
          totalShippingPriceV2 {
            currencyCode
            amount
          }
          totalTaxV2 {
            currencyCode
            amount
          }
          totalPriceV2 {
            currencyCode
            amount
          }
          processedAt
        }
      }
    }
  }
}

 

0 Likes
Highlighted
Excursionist
21 0 6

Any update on this? According to the https://www.howtographql.com/ tutorial referenced by Shopify 'last: 10' should work suggesting this is a Shopify thing... So, an answer/documentation would be appreciated.

 

The example:

 

https://www.howtographql.com/basics/1-graphql-is-the-better-rest/

 

 

 

However, this:

{
    customers(last:10) {
			edges {
				node {
					id,
          firstName,
          ordersCount,
          lastOrder {createdAt}
				}
      }
		}
}

Returns:

 

    {
      "message": "using last without before is not supported",
      "locations": [
        {
          "line": 10,
          "column": 5
        }
      ],
      "path": [
        "customers"
      ]
    }

 

Am I meant to pretend there is no 'last' and instead use 'first:10, reverse:true' instead?

 

Thanks.

0 Likes
Highlighted
Shopify Partner
14 2 1

Also having the same issue.  Error: using last without before is not supported

The documentation doesn't mention that.  It simply says: 

last ( Int )

Returns up to the last n elements from the list.

 

Any help would be appreciated.

0 Likes
Highlighted
Excursionist
21 0 6

@Codesto1  - this looks like it might be a workaround for the API bug:

 

'first:10, reverse:true'

But I've not tested it thoroughly. Ideally, 'last:n' would be implemented correctly.

0 Likes
Highlighted
New Member
3 0 1
'first:10, reverse:true'

is not equal to

'last:10'

 

 

 Imagine you have 20 data

[1,2,3....17,18,19,20]

The 1st approach will return

[10,9,8,7...3,2,1]

while the 2nd will return

[20,19,18....12,11,10]

 

 

0 Likes
Highlighted
Excursionist
21 0 6

Thanks for confirming my suspicions. So, we just need 'last:10' support, or at least documentation.

 

0 Likes
Highlighted
Tourist
6 0 2

bump thread

2 Likes
Highlighted
New Member
3 0 0

I am having this exact same issue.

Where the query
customer(customerAccessToken: "${customerAccessToken}") {
    orders(last: 10) {...}
}
 
returns: "message": "using last without before is not supported"
 
Any news on this issue?
0 Likes
Highlighted
New Member
3 0 0

What happen if we use the sortKey like this:

orders(first: 10, reverse: true, sortKey: CREATED_AT) {...}

Is that possible?

I posted this issue here https://stackoverflow.com/questions/62765178/graphql-error-using-last-without-before-is-not-supporte... and I got an answer from another dev already.

0 Likes
Highlighted
New Member
2 0 1

bump has anyone found a solution to this? 

1 Like