[Graphql Products] Query Products within Collections

Highlighted
Tourist
4 0 1

Hello! 

 I'm trying to get products within collections. Reading the docs I don't know if possible via GraphQL 

 

My idea is some sort of 

products(first:10, query:'collections_id or something to join with the collection'){
       { }
}

my approach is probably the wrong one but I hope you get the point.

 

0 Likes
Highlighted
Shopify Partner
1866 181 748

Hi @Aoliver98 

Think about it the other way around. What collection do I want to show products for? So first query the collection, then its products connection.

{
  collectionByHandle(handle: "casual-things") {
    products(first: 10) {
    	edges {
        node {
          id
          handle
        }
      }
    }
  }
}
Liked this post? You might also like our fantastic upsell apps Candy Rack and Candy Cart or offer free gifts with Gift Box. All made with ❤️  and care by Digismoothie
0 Likes
Highlighted
Tourist
4 0 1

Hi, thanks for the quick reply. @KarlOffenberger 

I will give you an idea of the application

I have a list of products, those products can be filtered by multiple collections and price range and can also be sorted alphabetically or by price.

Is there a possibility for this function "collectionByHandle" to search for products by price range?

0 Likes
Highlighted
Shopify Partner
1866 181 748

Not that I am aware of. The products connection doesn't have a query argument.

You're basically left with fetching all products within that collection and then filtering them client side using JS. Alternatively, query all products within a price range as shown below, then filter client side by collection they're in though that might be less efficient given the products > collection search space is much larger than if you'd go collection > products. Also note that any of these aren't really suited for large catalogs.

This is where you're better off using Liquid.

Liked this post? You might also like our fantastic upsell apps Candy Rack and Candy Cart or offer free gifts with Gift Box. All made with ❤️  and care by Digismoothie
0 Likes
Highlighted
Tourist
4 0 1

Thanks for the help.

I'm using Flutter with Graphql Client. 

So I'll to filter the price range in the client side and I'll see how it works

 

Have a nice day