Closed btodorce closed 3 years ago
you need to use this with another query aggregateUser
Off topic but since u obviously have more experience in this, if i wanted to create a aggregate field inside a model and do some aggregate function but not cause the N+1 issue. My approach extends the nexus object autogenerated by prisma-tools but in each iteration it fires a new query
In this example im trying each time i invoke findManyUser to retrieve there avg age from another model(db table) linked with a UserDetail
query findManyUser { aggregate }
export const UsersAvgAge = extendType({ type: 'User', definition(t) { t.field('aggregate', { type: 'AggregateUser', resolve(_parent, args, { prisma }, info) { const select = new PrismaSelect(info).value return prisma?.useDetail?.aggregate({ where: { id: { equals: _parent.id, }, }, ...select, }) }, }) }, })
@btodorce You need to know that our generated CRUD is a copy of Prisma Client API
If prisma findMany
API has aggregate
inside it so you will find in our GraphQL query
what you need here is using groupBy
This would be amazing if it can be done, ithe generator to generate aggregate field inside the actual query and return count,avg,sum,min,max for the underlying model
query { findManyUser { aggregate( avg { age: true } ) } }