Best way to query stock for a list of SKUs on specific locations

Hi all

We build custom warehouses solution and custom app.
I need to find the most optimal way to query stock in the app.

Our online shop works within context of selected store, which is Shopify Location.
This store has warehouses associated to it (other locations).
This is not the built-in Shopify association feature, its our custom.

Our front end can send parameters to the app API:

  • location gid
  • variant gid OR sku.

Now I have 2 possibilities to drill down to inventory item:

  1. using productVariant query by ID and asking for inventoryItem
  2. using inventoryItems query with filter query:"sku:<sku>"

Which one is will be faster?

Next, I need to ask for inventory levels at specific warehouses.
I can ask each warehouse individually, by creating named field for each, example:

    inventoryItem {
        _w1:inventoryLevel (locationId: "gid://shopify/Location/88008130813") {
          quantities(names:"available") {
            quantity            
          }
        }
        _w2:inventoryLevel (locationId: "gid://shopify/Location/65087111421") {
          quantities(names:"available") {
            quantity            
          }
        }      
    }

OR I somehow can use query, but due to Shopify-ish style of documentation I can’t figure if it is even possible. I’m sinking in endless errors, example of my code:

inventoryLevels(first: 1, 
          query: "location:id:'gid://shopify/Location/88008130813'") {

It will not accept “location.id”, “location:id” or anything like that as a filter. How to filter by location via “query” remains a mystery, but there is a chance this is doable.

Is there a possibility to use query and which pathway is better for performance?

Many thanks to everyone for your support

Hi @Dimitry_Nechaev

From digging into this it looks like your current alias-based approach is the best option. There’s no query filter for location on inventoryLevels - the supported filters don’t include location. Using the inventoryLevel(locationId: ID!) field with GraphQL aliases is the intended pattern for this use case.

1 Like

Thanks Liam. I almost tagged as solved when realised my first question still stays. Thanks for answering the second!

The productVariant(id) → inventoryItem approach when you have the variant GID should be faster because direct ID lookups are O(1) indexed operations, while SKU search queries require text matching.

Liam, you are the best, thanks mate

1 Like