Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Re: GraphQL Collection Filtering

Solved

GraphQL Collection Filtering

Richard_Peck
Shopify Partner
13 2 3

I am trying to filter products within a collection using the Storefront GraphQL API: -

 
query ($handle: String!, $filters: [ProductFilter!]) {
    collection(handle: $handle){
        products(first: 250, filters: $filters) {
            edges {
                node {
                    id
                    handle
                    title
                    descriptionHtml
                    productType
                    tags
                }
            }
            pageInfo {
                hasNextPage
                endCursor
            }
        }
    }
}
 
Vars: 

 

 

{
    "handle": "******",
    "filters": [{"tag": "*****"}]
}

 

 
I've redacted the specifics for privacy reasons.
 
There are two problems with this: -
 
1. The number of products returned is minimal. Pinging the "products" endpoint gives 40 products for a query using the above criteria, the above returns 12. It is a similar story for other collections and filters.
 
2. If I add more filter criteria, the system returns unexpected results. For example, stipulating two product tags returns products as if the tags are considered an "OR" query. I've just checked the documentation and this does seem to be expected behaviour...
 
or.jpg

In this case, how would I filter by two "tags"?
 
--
 
If anybody could provide insight into why only a certain number of products are returned, that would be greatly appreciated. The latter issue I can work with.
Accepted Solution (1)
Richard_Peck
Shopify Partner
13 2 3

This is an accepted solution.

After a lot of back-and-forth with technical support, it turns out they don't support filtering collections with a large number of products inside. Since the collection I wanted to filter had 7k products, this was causing the problem. Smaller collections should work fine.

View solution in original post

Replies 6 (6)

ShopifyDevSup
Shopify Staff
1453 239 534

Hi @Richard_Peck,

 

Thanks for your post. When testing just now in a test store with the Storefront API v2023-04 using the query that you specified all expected products are being returned with different combinations of a collection handle and a tag. 

 

If you're still having trouble with products consistently missing from the query please submit a support request through the Partner dashboard and use the API Support topic and Email path. Important details include a specific example from a store that you have staff or collaborator access to with details of both queries used (the product one as well as the collection one), which App is making the queries, which API version is being used, and a specific product that keeps getting excluded from the results even though it matches the filter.

 

For the part about tag filtering, if you're looking for an 'AND' type filter on multiple tags setting up the variables this way works:

 

{
  "handle": "test-1",
  "filters": [
    {
      "tag": "skis",
      "tag": "Large"
    }
  ]
}

 

Hope you have a great day,
Jon551

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

Richard_Peck
Shopify Partner
13 2 3

Many thanks for the comprehensive response!

 

Let me send a support request in the partner dashboard. I'll also check your suggestion regarding the tag filters. I am quite sure I did test that but will look again.

 

I'll provide an update having done so.

Richard_Peck
Shopify Partner
13 2 3

Nope, filters not respecting "AND" functionality at all. Will detail it further in partner dashboard support ticket. Thank you for your help.

TerraIncognita
Shopify Partner
3 0 0

@Richard_Peck did you find a solution to this you could share? We're hitting the same issue and looking for a non-hack solution. 

Richard_Peck
Shopify Partner
13 2 3

This is an accepted solution.

After a lot of back-and-forth with technical support, it turns out they don't support filtering collections with a large number of products inside. Since the collection I wanted to filter had 7k products, this was causing the problem. Smaller collections should work fine.

TerraIncognita
Shopify Partner
3 0 0

@Richard_Peck Actually, I found this suggestion to work! Maybe an update since this post, but it works on 2023-10 at least.