I'm looking to get the largest order a customer has made. For example, customer made 100 orders throughout last 2 months and we want to find the one with the largest amount.
Of course the trivial solution is paging through all order history and then finding the largest order but that's a very wasteful query (time and API cost) and was wondering if anyone had a smarter idea.
We invented computers specifically to do that junk work people used to do.
You would be wise to accept that premise and carry on. You are correct in that if you ask for a customer record, you can also get all their orders. In fact, with GraphQL you an get that in ONE call. With a second call you can poll for the results, and when ready, you get a link to all the JSONL data with all the customer orders. Now you can use loop to scan the JSON looking for the largest total_price of the orders, and that result is your desired largest customer order. Some things to note.
- you made 2 API calls to get ALL the data you need. If you call that wasteful, fine, but your credibility is minimal in arguing that point.
- most of your effort is a loop and compare statement. Again, the most common pattern of using a computer to figure something out. Why fight it
So there you go. No waste, fuss, muss or headaches.
You could have simply answered - "No, no way to get max order w/o scanning all orders" (as opposed to total spent or avg order amount which are pre-calculated by Shopify).
When you need to call "get all orders" for multiple customers at scale in realtime it does have an impact on API cost that can result in errors and/or UX impact.
The time you invested in writing the rest of your condescending answer was unnecessary.
Thanks for the advice Shaibt. You could simply say Thank You for me taking the time to explain and present the right answer to you, without a lecture on your feelings. After all, your words started this. Touché dude. Have a nice day.
For all those still confused ----> Use the Bulk Dowload Query pattern to avoid API calls.