[Graphql Products] Query Products within Collections

Aoliver98
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.

 

Replies 5 (5)

KarlOffenberger
Shopify Partner
1873 184 901

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
        }
      }
    }
  }
}
Aoliver98
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?

KarlOffenberger
Shopify Partner
1873 184 901

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.

Aoliver98
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

 

 

 

zainmirza60
Shopify Partner
33 0 2

yes working with filter collection but lost the query parameter on product connect I do not know why shopify not allowed the query parameter in second connection.