paljs / prisma-tools

Prisma tools to help you generate CRUD system for GraphQL servers
https://paljs.com
MIT License
681 stars 55 forks source link

Feature Request: Aggregates args field to models #209

Closed btodorce closed 3 years ago

btodorce commented 3 years ago

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 } ) } }

AhmedElywa commented 3 years ago

you need to use this with another query aggregateUser

btodorce commented 3 years ago

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, }) }, }) }, })

AhmedElywa commented 3 years ago

@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