Hey ppl, is there a way to count relation nodes in...
# orm-help
n
Hey ppl, is there a way to count relation nodes in prisma queries?
Do I need to create a separate field to handle count of nodes?
on graphcool it seems that it was possible using the following query
Copy code
_modelMeta {
      count
}
n
count
is available through the
aggregate
field of a connection query 🙂 https://www.prisma.io/docs/prisma-graphql-api/reference/queries-qwe1/#connection-queries
n
I couldn't use it with relations... Does it work with relations? I know that
aggregate
works if you want to count the current model in query
n
Can you give an example for what you want to do? (please include the relevant types from the datamodel 🙂 )
n
Something like this
Copy code
type Brand {
  id: ID!
  name: String!
  products: [Products!]
}

type Product {
  id: ID!
  brand: Brand!
}

query {
  brands(where: {
    name: "BrandName"
  }) {
    products {
      count
    }
  }
}
This allows you to query that aggregate every-time you want it in the same type
n
hmm... got it. I thought there was a way to do it directly from query
h
This might get an improvement in datamodel v2 but right now this is the way 🙂
n
Ok, thanks for your attention @Harshit and @nikolasburk
👍 1
@Harshit One more question, If I need to get the list of brands with the products count for each one, considering I have lots of brands, maybe it should be better to save the count on Brand model, right? Otherwise I need to execute a query for each of the brands. What are your thoughts on this scenery?
h
you can add it to product for the above query. The count is calculated in time using index.
I am guessing that you are saying that you need to store the count?(answer is no)
n
Yeah, I thought it would be necessary to store the count value in the model.. This way I can orderBy the brands with more products