Graphql - How to get total product count in a collection?

mattbadal
New Member
1 0 0
Here's how I have mine set up. The problem is, it only gets the first 250. What if I don't know how many products there are in a collection and want to get all the products?
 

 

 

 

{
              collectionByHandle(handle: "${collectionName}") {
                    products(first: 250) {
                      edges {
                        node {
                          id
                          title
                          description
                          tags
                        }
                      }
                    }
                  }
              }

 

 

 

 
0 Likes
csam
Shopify Staff
Shopify Staff
34 5 4

Hi @mattbadal 

You'll want to paginate through the results using the pageInfo field. You can read more about this on our blog - https://www.shopify.ca/partners/blog/graphql-pagination as well as on our GraphQL documentation https://shopify.dev/api/usage/pagination-graphql This allows you to step through all the products in the collection by the amount you specify in the first field. 

If you're just looking for the count of products in a collection, you can use productsCount. So for your example, you'd put it at the main level of the query like so: 

 

{
              collectionByHandle(handle: "${collectionName}") {
                   productsCount
                    products(first: 250) {
                      edges {
                        node {
                          id
                          title
                          description
                          tags
                        }
                      }
                    }
                  }
              }

 

 

CS | API Support @ 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 Shopify.dev or the Shopify Web Design and Development Blog

0 Likes