I’m currently working on a project using Shopify’s GraphQL API, and I’ve successfully used the productsCount query to get the count of products in the store. However, I now need to get the count of product variants.
Is there a specific query in GraphQL to achieve this, similar to the productsCount query? If so, could someone please provide an example of how to construct this query?
Any help or guidance would be greatly appreciated!
You coiuld use graphql1 to get the large of the productVariants. if the nodes number is less than 250 ,then you could use nodes.length to get the count
Thank you for your response. Unfortunately, the solution provided does not meet my expectations. I believe a better approach would be to count product variants using the products query, as it includes the variantsCount field. We can sum the variantsCount per page while paginating the products. This method requires less pagination if multiple variants are tagged in a single product.
In my opinion, using the products query is a better way than using a productVariants query. However, I am looking for solutions similar to the productsCount query, which do not rely on pagination. Is there a substitute for counting product variants?
Below is the productsCount example for reference:
{
productsCount {
count
}
}
I appreciate your assistance in finding a suitable solution.
Thank you for your response. It has been very helpful. However, I’m not entirely sure if the approach you mentioned has a really higher efficiency in terms of Shopify’s internal implementation compared to directly querying product variants through pagination.
I’m concerned that querying products and calculating the total number of variants for each product may trigger additional internal queries within Shopify. As you said ,maybe there is better approach to do this