Find Draft Order Line Items that contain a particular SKU

Solved
Highlighted
New Member
12 0 0

We have the following problem: all of our products in shopify are unique objects, i.e. the quantity will always be 1 for any product, but multiple draft orders can be created for the same product. We use draft order creation and deletion to track item status in our inventory system, but since multiple draft orders can be created for a product, we need to check to see if any other draft orders exist for that product when a draft order deleted event is delivered to know which state it should be in on our side. We use SKUs to match shopify products to our inventory items.

tl;dr: I need to find draft order line items by sku, but the GraphQL docs indicate that `lineItems` does not support a `query` parameter: https://shopify.dev/docs/admin-api/graphql/reference/object/draftorder?api[version]=2020-04

How can we solve this problem? I see no way to find draft orders that contain a line item with a particular sku (which I really feel it should support). Is there some alternate way I can do this?

If it helps, we can trivially access the product and variant IDs.

0 Likes
Highlighted
New Member
12 0 0

After thinking about it more, I guess since the draft order line item is not an object in the query root, there would be no way to do this. I guess we will have to track draft orders on our side, but I'd much rather use Shopify as the source of truth than our database.

0 Likes
Highlighted
Shopify Staff
Shopify Staff
587 72 130

This is an accepted solution.

Hey @parkerjm,

I agree that since it's not currently possible to filter draft orders by line_items, it makes more sense to keep an index on your side and reference that instead. You can keep your index up to date by utilizing webhooks, bulkOperations, or a combination of both.

Using the draft_order#create and draft_order#update webhooks, you can be notified in real time when items are added/removed from drafts.

Our bulkOperation endpoint allows you to write a single query to get large amounts of data from a store, so you can use this to return a list of all items that currently exist on draft orders in a given shop. The query could look something like this:

mutation {
  bulkOperationRunQuery(
   query: """
         {
  draftOrders {
    edges {
      node {
        id
        createdAt
        updatedAt
        lineItems {
          edges {
            node {
              sku
            }
          }
        }
      }
    }
  }
}
    """
  ) {
    bulkOperation {
      id
      status
    }
    userErrors {
      field
      message
    }
  }
}

JB | Developer Support @ Shopify
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Click Accept as Solution 

0 Likes
Highlighted
New Member
12 0 0

Thanks, JB. We currently receive the webhook callbacks already, so we're good there, but I will use the bulk op to backfill existing draft orders.

0 Likes