How to Query Product Variant Count in GraphQL?

How to Query Product Variant Count in GraphQL?

deepak_ds
Shopify Partner
3 0 0

Hi everyone,

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!

Thank you!

Replies 5 (5)

Liam
Shopify Staff
2831 311 806

Hi Deepak,

 

Would the productVariants query work for you? eg:

 

query GetProductVariantsCount {
  productVariants(first: 1) {
    pageInfo {
      hasNextPage
    }
    edges {
      cursor
    }
  }
}

 

Liam | Developer Advocate @ 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

deepak_ds
Shopify Partner
3 0 0

Hi Liam,

No, this doesn't work. I have asked for a solution on Eric-HAN reply.

Please refer that response

I appreciate your assistance in finding a suitable solution.

Eric-HAN
Shopify Partner
94 15 7

Hi, there 

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

query GetTotalProductVariants {
  productVariants(first: 250) {
    edges {
      node {
        id
      }
    }
    pageInfo {
      hasNextPage
    }
  }
}

.if the could is larger than 250 .or If the hasNextPage field is true , you will need to paginate through the results by using the after cursor. 

query GetTotalProductVariants($after: String) {
  productVariants(first: 250, after: $after) {
    edges {
      node {
        id
      }
    }
    pageInfo {
      hasNextPage
      endCursor
    }
  }
}

 Just doing this  fetching until hasNextPage is false and count the total number of variants by summing up the number of edges returned in each query

- Helpful? Please hit Like and mark it as a solution
Want to modify or custom changes on store? Let me help.
- Feel free to Email Me    Buy Me A Coffee
deepak_ds
Shopify Partner
3 0 0

Hi Eric_HAN,

 

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.

Here is an example of the query:

 

{
  products(
    first: 250
  ) {
    nodes {
      variantsCount {
        count
      }
    }
    pageInfo {
      hasNextPage
      endCursor
    }
  }
}

 

 

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.


Thanks,

Deepak Singh

Eric-HAN
Shopify Partner
94 15 7

Hi,there

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

- Helpful? Please hit Like and mark it as a solution
Want to modify or custom changes on store? Let me help.
- Feel free to Email Me    Buy Me A Coffee