Hello guys, quick question ? How can I select cust...
# orm-help
t
Hello guys, quick question ? How can I select custom fields with prisma not only existing ones? For instance I want to get average between X and Y columns and make custom column with that value.
m
Hello @Tarik Brat Are you using prisma in a GraphQL or a Rest API ? If you are using GraphQL you could use a resolver for that average calculation. Like so (with nexus) export const Foo = objectType({   name: 'Foo'_,_   _definition(t) {_ _    t.nonNull.int('x')_ _    t.nonNull.int('y')_ _    t.nonNull.int('average', {_       _resolve: async (parent) => {_         const { x, y } = parent         return //...       },     })   }, })
💯 1
t
I'm actually using it for Rest API
m
Sorry I'm a bit late. For a Rest API, I recommand creating a helper function that will take x and y as args and return your average. Then you can merge this result to your actual object with spreading like this :
Copy code
async (req, res) => {
  const foo = prisma.foo.findUnique(...)
  const average = caculateAverage(foo.x, foo.y)
  res.send({ ...foo, average })
}