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