Hey all, super excited to spend the weekend learni...
# orm-help
m
Hey all, super excited to spend the weekend learning more about Prisma / graphQL. I'm wondering what is the best way of grabbing the total count of related nodes in a query? For example, if I have a query like this:
Copy code
query GET_CATEGORIES{
	categories {
    name
    id
    description
    posts {
      id
    }
  }
}
What I really need from the posts is the total count of the posts assigned to each category. Right now I'm just calling posts.length on the returned array of ID's but I'm guessing that's not particularly future proof / scalable. Any help would be massively appreciated!
j
I think you can use
aggregate
for this, docs are here: https://www.prisma.io/docs/prisma-client/basic-data-access/reading-data-TYPESCRIPT-rsc3/#aggregations Example
Copy code
catgeoriesCount(parent, args, ctx: Context, info) {
    return ctx.db.query.catgeoriesConnection({}, info);
  }
Copy code
query categoriesCount {
    categoriesCount {
      aggregate {
        count
      }
    }
  }
Hope that helps!
m
I may well be mis understanding something but I'm not sure that applies in this situation since that would just get the count of the categories. I'm trying to get the count of Posts that are related to each category.
y
use
where
. e.g.
where: {category : "horses"}
m
@yolen thanks for the input! Would that not mean then that I need to send a separate query for each category? There may be any number of categories returned from the first query so I'd like to avoid creating another network request for each if possible.