Query products by collection

Tourist
7 0 1

Using graphQL, is it possible to query products by the collection they belong to? Sililar to this:

 

{
	shop {
		products(first:250, query:"collection:'collection-handle' tag:'tagValue'") {
			edges {
				node {
		          id
		          title
		          description
		       }
			}
		}
	}
}

Or perhaps as a subquery like this:

 

{
	shop {
		collections(first:250, query:"handle:'collection-handle'") {
			edges {
				node {
					products(first:250, query:"tag:'tagValue'") {
						edges {
			    			node {
				              id
				              title
				              description
				            }
						}
					}
				}
			}
		}
	}
}

 

Neither of these work with the former responding with ""Field 'products' doesn't accept argument 'query'""

0 Likes
Shopify Partner
1843 170 537

Hi James,

The second query where you use the query filter on collections won't work because you are providing an unsupported filter parameter.

Otherwise, yes, that is possible using a specific query root field collectionByHandle

query {
  collectionByHandle(handle: "collection-handle") {
    products(first: 250) {
      edges {
        node {
          id
          title
          description
          tags
        }
      }
    }
  }
}

No, you will not be able to filter the products by tag within that same query because the product connection on the collection object isn't equivalent to the product connection on the query root (check both in docs and you will see).

Hope this helps!

I turn coffee in to code - since 1998
0 Likes
Highlighted
Shopify Partner
133 4 12

Hi Karl, looking for an efficient graphql way to get multiple products by handle and multiple collections by handle, would you have any idea if this is possible?

 

 

0 Likes