How to get list with all its references

Hi there,
My first question at DatoCMS community. Help me if something is wrong or not clear.

I have 2 models: products and product variants (such as the country that made this product: China, India, UK, etc). Product link to the product variant.

No problem with the product query to list all products in CMS.
My challenge is a summary and counts the number of each product that belongs to each variant. For example:

  • China (10)
  • India (50)
  • UK (0)

    150 variants

I use listPagedIterator() to get all the variants in the variants model and then query each record to get all the reference products belonging to each variant. This resulted in me making over 150 requests to the CMS.
I know this is not optimal and not good but there is no way to do this yet.

Any suggestions for me? Thank

Hello @pmtandhqn and welcome to the community!

From what i understood you are trying to count how many products you have for each variant.
For that, i think it would be best for you to use our GraphQL API instead of our rest API.
To see how to use our GraphQL API you can read here: Content Delivery API - Overview - DatoCMS Docs

More specifically, i think the _allProductsMeta request, that can show you how many products you have through the count parameter is exactly what you are looking for.

You can read about that at the bottom of this page here: Content Delivery API - Pagination - DatoCMS Docs

Thanks @m.finamor . But seem like my describe doesn’t clear. I thought the solution was like this:

  • I have a list (more than 150 countries in the world). First, I need to call a request to get list of all support countries in my store from Countries model.
  • Second, call more than 150 request to Product model with _allProductsMeta.

The difference here is that instead of me calling Rest API your suggestion is Graphql API. Does it make much of a difference? Because actually I also have to loop and call over 100 requests to DatoCMS

Oh sorry @pmtandhqn, i thought you were using the GraphQL API already.

Through the REST API the iterator paginates for you automatically, so it won’t do 100 requests to get 100 records, it will paginate as described here as well: Pagination - DatoCMS