Have your say in Community Polls: What was/is your greatest motivation to start your own business?
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.

Product count using Graphql.

Product count using Graphql.

parth-gondaliya
Shopify Partner
22 0 9

How to get product count in the shop using Graphql?

We can get it from REST but I couldn't found in GraphQl.

 

PLEASE HELP!!!

Replies 9 (9)

Gregarican
Shopify Partner
1033 86 292

I don't believe GraphQL can give you a one-shot count of all products in a Shopify store. The REST API call works, is pretty basic to implement, so I'd suggest just using that in your project. Figure there are no query parameters to utilize, no request body to construct, etc. Just invoke the proper URL, provide your API credentials, and the response will come back!

JoshHighland
Shopify Partner
213 12 77

With the recent depreciation of the REST api for products, we don’t have a solution for getting a product count now. 

SEO Manager - The all-in-One SEO solution for Shopify
A powerful suite of SEO tools that gets you found in Google results

- Install SEO Manager -
ShopifyDevSup
Shopify Staff
1453 238 524

Hi @parth-gondaliya and @JoshHighland,

 

With the deprecation of the REST Product API, there is also a new GraphQL Admin API query that was released in version 2024-04 that does let you retrieve a count of all your products, though it is limited to returning a maximum of 10,000. The productsCount query doe also have a query filter that you can use to work around the 10,000 count limitation by making multiple queries, for example you can query for all products created before and after a certain date in separate calls to get the full count of your products.

Additionally there is also a variantsCount field on the Product object, to retrieve the amount of variants each product has as well.

 

I hope this helps, and I hope you have a great day 🙂

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

BogdanM
Shopify Partner
123 3 29

This "work around for the 10,000 count limitation" is not working.
What if the store has 20,000 products created in one day. How could we use a query on date in this case.
We'd have the same issue on any query filter.
We should not need any knowledge about the product data to get a count on all products.

Bogdan | WebShopAssist
User friendly apps with top-rated support
- SKUGen the SKU generator
- DPD Integration
ShopifyDevSup
Shopify Staff
1453 238 524

Hi @BogdanM,

 

You should still be able to use the created_at query filter in the case that 20,000 products were created in a single day, as the created_at filter does accept a timestamp value with accuracy down to the second. For example if you created 20,000 products on May 1st, starting the import at 12:00:00 UTC, and the import ran until 13:00:00 UTC, you can use a timestamp at 12:30:00 UTC to split the results of the query in half essentially. Here's an example that shows the created_at filter being used in this way.

{
   productsCount(query: "created_at:>'2024-05-01T12:30:00Z'") {
       count
       precision
   }
}

Additionally there are a number of other filters that you can use to split the results of the productsCount query, for example you can use the title to filter results alphabetically with the query: "title:>'N'" returning a count of all products with titles that occur aphabetically after the letter N. You can also use filters like ID, SKU, Barcode and more in this same manner.

You can refer to the productsCount page in the Shopify.dev docs for a comprehensive list of all the values you can filter this query by. You can also see more information about how filter queries work with specifics in the Shopify API Search Syntax.

We do understand how this 10,000 count limitation can be inefficient and limiting in how you retrieve product counts on a store going forward, and I will absolutely be sending up some feedback on your half to our developers regarding this. We always take our merchants and partners feedback to great value when working on improving and updating the platform in the future, and the more feedback we get for topics like this, the quicker and more likely they are to be actioned.

I hope this helps, and I hope you have a great day 🙂

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

BogdanM
Shopify Partner
123 3 29

Hi @ShopifyDevSup 

 

The solution you are describing assumes that we know how the product date looks. 

Why should we know between what times to query or what letter the products start with in order to get the count.

Most of the time this is impossible to know.

To every example you offer I can find a counter example that breaks it, because it rely on product data.

 

What I mean is that there should be a way of getting the total product count without knowing how the products can be partitioned in chunks of less than 10000.
I shouldn't be this difficult, isn't it? 🙂

Bogdan | WebShopAssist
User friendly apps with top-rated support
- SKUGen the SKU generator
- DPD Integration
gmlewis
Shopify Partner
1 0 0

When I attempt to make a `productsCount` query like this:

{
  productsCount {
    count
  }
}

I get the response:

{"errors":[{"message":"Field 'productsCount' doesn't exist on type 'QueryRoot'","locations":[{"line":2,"column":3}],"path":["query","productsCount"],"extensions":{"code":"undefinedField","typeName":"QueryRoot","fieldName":"productsCount"}}]}

 Any ideas what I'm doing wrong?

GrantM21
Shopify Partner
1 0 0

@gmlewis  I'm not exactly sure how to interpret the error you got, but are you able to query anything about products? If not, it could be an issue with your access scopes.

 

You can run this query to see what access scopes you currently have:

query {
  appInstallation {
    accessScopes {
      handle
      description
    }
  }
}

This is what it returns for me:

"data": {
  "appInstallation": {
    "accessScopes": [
      {
        "handle": "write_products",
        "description": "Modify products, variants, and collections"
      },
      {
        "handle": "read_products",
        "description": "Read products, variants, and collections"
      }
    ]
  }
}

From my understanding, you need at least "read_products" access scope to run the "productsCount" query.

 

To update your access scopes, you can update the `shopify.app.toml` file in your root project directory to include "read_products" in the scopes section like this:

[access_scopes]
# Learn more at https://shopify.dev/docs/apps/tools/cli/configuration#access_scopes
scopes = "read_products,write_products"

 

The docs go into this more in depth here. Hope this helps if this is your issue.

mejuddhav
Shopify Partner
1 0 0

Check API version