GraphQL get all orders containing a discount code

Hello, i’m building a simple dashboard where ambassadors from a shop that i help to manage can see all the orders made using their custom discount code. The problem i have is when an order has used more than one discount code, included the one i’m searching for, and the GraphQL api doesn’t return it to me cause it only gets the first discount code. The easieast solution is to grab all the orders and filter them server side but since they are 3500 now and they increase daily this way is definetely not possibile. I could store a local cache of the order in my db but i would like to avoid it since i’m trying to keep it as small as possible. Is there any othe way i can follow?

@Bellets the order search index only keys off the first discount code, thats the limitation youre hitting. so dont filter by code at query time at all. read the full discountCodes array instead (it lists every code on the order, not just the first) at write time, and record the match as an order tag.

set up an orders/create + orders/updated webhook, check if the target code is in discountCodes, and if it is, tag the order like ambassador-CODE with tagsAdd. then your dashboard just queries orders with query: "tag:ambassador-CODE", which is properly indexed and handles multi-code orders fine. no local order cache needed, the tag lives on the shopify order.

for the 3500 you already have, run a one-time bulkOperationRunQuery export of orders with their discountCodes, filter, and tag the matches. after that the webhook keeps it current on its own.

Hey @Bellets ,

As far as I know if the GraphQL search only indexes the first discount code there isn’t a way to query orders by secondary discount codes directly.

For a scalable solution I’d consider using order webhooks to capture new orders and store only the minimal data you need such as the order ID and all applied discount codes. That avoids repeatedly querying thousands of orders while keeping your local data relatively small.

I’m not aware of a native GraphQL filter that searches across all applied discount codes but I’d be interested to know if anyone has found one.

Thank You !

Thank you all for your answers. I will try the solutions you gave me!

Don’t process already processed data.
You don’t have to cache all of an order just the ID; unless your trying to be faster , avoid network call, etc.
Don’t bulk process each time when you can work on a single at time of order creation/update with webhooks.

Use tags on the order to flag if it’s been processed by your app before in future editing.
And or a tag IF it also has a discount.
Remember merchants, other apps or services etc can remove your tags build logic accordingly.
Technically you can offload this process with a Flow template extension or through guiding your customers to set it up but if that doesn’t run your using bad info /shrug.
Then use webhooks to double check any edited/updated orders to set/delete the tag.

There’s also app data metafields but you need to verify it’s usable in any queries being used.
If so then you might just be able to streamline even more with a metafield key per entity.