We are in the process of migrating from the REST Admin API to the GraphQL Admin API.
In the REST API, we would use the discount_codes
field in the order to determine the code used and amount of the discount attributed by that code. The same information does not seem to be available in the GraphQL API.
The discountCodes
field is just a list of strings (list of codes, no amounts) and the discountApplications
contain an amount
but no code that was used.
Relevant links:
Hi Michael,
Thanks for highlighting this - it would be great to learn a bit more about your use case. When using the GraphQL API, is it not possible for you to query both the discountApplications
field and the discounts_code
field on the order object so you can get both pieces of data?
Is the issue that you’re unable to determine which discount code corresponds to which amount that’s received from the discountApplications
field?
Once we understand more about the specific challenge you’re facing we’ll be able to deterine the best options for supporting your migration.
Hi Liam,
You nailed it, that is exactly the issue. We are able to get the amounts via discountApplications
but are not able to map them to the corresponding discount code.
Thanks for explaining this - have connected with our product team, and I’ll update here when I learn more.
Just to make sure we’re on the same page, I want to bring up awareness of GQL interface types which need to be called in a special way using fragments.
Now bringing this back to the Admin GQL API, you can see that the Order.discountApplications
connection ultimately points to an interface type. At the bottom of the page for that interface you can see all of the implementing types and the one you want is the DiscountCodeApplication
type.
Putting this all together your query would look something like:
{
order(id: <id>) {
discountApplications(first:10) {
nodes {
value {
... on MoneyV2 { ... }
... on PricingPercentageValue { ... }
}
... on DiscountCodeApplication {
code
}
}
}
}
}