A space to discuss GraphQL queries, mutations, troubleshooting, throttling, and best practices.
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.
Solved! Go to the solution
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
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