Query Products by Assigned location

Hi Guys

Currently, if I want to get all products that have been assigned to a specific location I have to do a bottom up query, meaning I have to query the variants where the location_id is the specified location and then get the product information for the variant.

This leads to returning a bunch of redundant data, and I am limited to returning only 250 variants per page, whereas if I could specify the location_id on products, I could return 250 products and then also on the variants query specify the location id, and return only those variants of a product that have been assigned to the specified location. This makes querying more efficient by returning only those products assigned to the location as well as only those variants enabling me to return way more data per request and drastically reducing redundant information.

Is that something that is achievable or because of data structure decisions too complex?

Cheers,
Gary

1 Like

Hey @Gary_Gilbert :waving_hand: - hopefully I’m understanding correctly here, but are you looking for something like this?

query getProductsAndVariantsAtLocation($locationId: ID!, $cursor: String) {
  location(id: $locationId) {
    inventoryLevels(first: 100, after: $cursor) { 
      pageInfo {
        hasNextPage
        endCursor
      }
      edges {
        node {
          item {
            id
            variant {
              id
              title
              sku
              product {
                id
                title
                handle

              }
            }
          }
        }
      }
    }
  }
}

This would give you a response like this:

{
  "data": {
    "location": {
      "inventoryLevels": {
        "pageInfo": {
          "hasNextPage": false,
          "endCursor": "eyJsYXN0X2lkIjo1NzcxMzA3MDUzODc3NCwibGFzdF92YWx1ZSI6IjU3NzEzMDcwNTM4Nzc0In0="
        },
        "edges": [
          {
            "node": {
              "item": {
                "id": "gid://shopify/InventoryItem/45931529175062",
                "variant": {
                  "id": "gid://shopify/ProductVariant/43833554862102",
                  "title": "Default Title",
                  "sku": "1234535",
                  "product": {
                    "id": "gid://shopify/Product/8013559595030",
                    "title": "Alan t-shirt",
                    "handle": "alan-t-shirt"
                  }
                }
              }
            }
          }

This should give you the inventory item ID and its respective variant info for a specific fulfillment location.

Hope this helps - let me know if I can help out further!

HI @alan_g,

No, I can already do that and thats what the AI Bot gave back its also what I call bottom up, because you are starting with the lowest connection and working your way back up to the product.

In your example case, for each variant you get duplicated product information and you get at maximum 250 variants back. What I want to do is top down. First starting with the product and working down to the variant, there is already a location_id filter on the variants.

something like this

query getProductsByAssignedLocation( $first:Int!){
    products(first:$first, query="location_id: 1234567889"){
      nodes{
         id,
        title
       variants(first:100){
          id
       }
      }
   }
}

This way I can get 250 products that have variants assigned to the location and up to 100 variants per product thats assigned to the specified location, giving me a potential 25.000 variants in one request with zero redundant information, where working bottom up you get the same product id, and title for each returned variant, and a maximum of 250 variants per request.

I would be even happy with also using the location_id filter on the variants connection at the samte time, but currently its not possible to filter products by location_id like it is variants.. you are already have the filter one level up on variants, it would be great to go just one more level up to the products :slight_smile:

My suggested approach is about 100 times more efficient use of a request.

Cheers,
Gary

Hey @Gary_Gilbert – thanks for clarifying, and thanks for waiting on my delayed reply here (I was away last week).

I took another quick look and, at the moment, from what I can tell, you are right that the Admin API doesn’t let us filter products directly by location_id. My understanding is that this is because inventory/stock related data is modelled at the InventoryLevel → InventoryItem layer (which can be accessed on a variant object as the parent), so any query still has to start there and “fan-out” to the parent product. This does definitely mean a much lower per-request ceiling points-wise when compared with the top-down pattern you’re proposing there.

I’m going to dig deeper and confirm whether there’s a known workaround I’m missing, or if we have anything on the roadmap to expose a product-level location filter. If not though, I’l log your use-case with the product team and set up a feature request for sure, since like you mentioned, your suggestion would definitely make calls like this more efficient.

I’ll update this thread as soon as I have more information. Thanks again for raising this - speak with you soon!