Hello,
I have a articles model with a one to many category association. I would like to count the amount of articles for each category but there doesnât seem to be a way to do aggregations. Does anyone have a solution to this?
Thank you very much in advance!
hello @sysadmin welcome to Community 
I think that you can do what you need by querying the _allArticlesMeta
(for example, not sure whatâs your model name), filtering with the IDs of the category and getting the count, something like this I guess:
_allArticlesMeta(filter: {cateogry: {anyIn: "123"}}) {
count
}
Ok, then two queries are required to do this, and it is not possible to do it using just one?
Hi @mat_jack1, Iâm currently facing a related issue, which is why Iâm posting in this thread:
I have a list of âcuratorsâ, each of these curators has a couple of curated âproductsâ and âplacesâ. As this is actually a many-to-many relationship (e.g. multiple curators can curate the same product and one product can have multiple curators), the curator â product / place relationship is stored inside the product / place model as a âsingle linkâ relationship (e.g. product / place â curators).
I now want to show a filterable, paginated list of all curators on the page with the number of curated products / places showing up next to the respective curator. It should look like this:
Now comes the problem: Executing the following GQL statements more than ~6 times inside a single GQL query creates a âmaximum complexityâ error:
places_${curator.id}: _allPlacesMeta(filter: {curators: {anyIn: ${curator.id}}}) {
count
}
products_${curator.id}: _allProductsMeta(filter: {curators: {anyIn: ${curator.id}}}) {
count
}
As the list is filterable, I cannot pull in the data at build time but need to populate the list during runtime. This also means that running a single GQL query for every item in the list would make the page much slower and would massively increase the number of API requests sent to Dato.
Do you have any suggestion of how to solve this situation? Many thanks for your support!
hey @oliver.lukesch what I would do is download all the products and places and then do all the counts in my frontend code. I think itâs much faster in the end and you can cache all the results so that the filtering/pagination is snappy.
Unfortunately I cannot think about a GraphQL-only workaround for you.
Hi @mat_jack1 , thanks for the quick reply. Yes, doing build-time processing is a possibility (we are already doing it in some other context) but I was hoping for a more âelegantâ solution. Still, many thanks for your feedback!
Are you planning on integrating bidirectional data linking and counts inside of model queries (like: city { curators { count } } }
at some point?
uhm, maybe you can add a comment to this feature request: Deep filtering on both CMA and CDA as it seems related to me?
If not please open a new one! Thank you!