GraphQL and collection inventory

Highlighted
New Member
1 0 0

Hello!

 

I want to build a "only show products in stock" filter using the GraphQL Admin API.

 

I can do this when I use the products root query like this, which is exactly what I want/need:

 

products(query:inventory_total<1) {
    title,
    description
}

However when I want to do the same with collections, the inventory_total query isn't a valid option for collectionByHandle. :(

 

DOES NOT WORK:

collectionByHandle(handle: "shoes") {
    products(query:inventory_total<1) {
        title,
        description
    }
}

 

Now I know I could query products using the collection_type field but then I lose sortKey options like price and best_selling which I also need.

 

Seems like a gap in functionally and was wondering if anyone knows a way to return a collection with just in stock items and not just using the products root query as it has limited sorting options.

0 Likes
Shopify Staff
Shopify Staff
1118 78 156

Hey @CodeGoblin ,

 

To my knowledge the query param doesn't work to filter products within a collection at all, so there wouldn't be a day to do this I'm afraid. This is done because the queries that need to be performed to filter that way can be extremely expensive, so for performance reasons we don't allow it. 

 

If you needed to filter based on inventory I think the way you had been doing it with the products root query is the way to go. Or, alternatively, you could still query products within a collection, and then add 'totalInventory' as a return value of the products and then filter ones that are out of stock manually. 

0 Likes