Section 09 of 14
Shopify API
What This Does
We use Shopify's GraphQL Admin API to fetch full order details — specifically the discount allocations and line item prices that the webhook payload doesn't include in enough detail. The GraphQL query fetches the order's currentSubtotalPriceSet (the post-discount total we compare against) and all line items with their discount allocations.
Why It Matters
The webhook payload tells us an order has an order-level discount, but it doesn't give us the precise allocation amounts per line item. We need the GraphQL API to get the exact discount breakdown for accurate ratio calculation.
How It Works
- 1.We find the most recent Shopify session (OAuth access token) for the shop domain.
- 2.We make a GraphQL request to Shopify's Admin API with the order ID.
- 3.The query returns: order name, currency, currentSubtotalPriceSet (post-all-discounts total), shipping address, and all line items.
- 4.Each line item includes its discount allocations — how much of each discount applies to this specific item.
- 5.We use the allocation data to sum up order-level discount amounts for the ratio calculation.
The Code
Key Decisions
- ●We query currentSubtotalPriceSet (not subtotalPriceSet) because it reflects the current state of the order including any post-creation edits.
- ●We use GraphQL instead of REST because we can get exactly the fields we need in one request, rather than multiple REST calls.
What Could Go Wrong
- ●If the Shopify session token has expired (rare, but possible if the app was uninstalled and reinstalled), the API call will fail with a PERMANENT error.
- ●Shopify rate limits GraphQL to 1000 cost points per second — our queries are lightweight so this isn't a concern in practice.