GraphQL and collection inventory

Solved
CodeGoblin
Shopify Partner
7 0 3

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.

Head of software development at Goblin Gaming
Accepted Solution (1)
Josh
Shopify Staff
Shopify Staff
1134 84 233

This is an accepted solution.

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. 

Josh | Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit the Shopify Help Center or the Shopify Blog

View solution in original post

Reply 1 (1)
Josh
Shopify Staff
Shopify Staff
1134 84 233

This is an accepted solution.

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. 

Josh | Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit the Shopify Help Center or the Shopify Blog