How to organise output types?
I'm having a hard time trying to figure out what the best way to organise output types and I was wondering if you guys/gals have any tips.
Things I'm having a hard time with:
- Some output objects that may be returned in multiple places (e.g.
users.get
, users.getAll
, posts.get
(as Post.author
), etc.) all need the same extra fields that can't come directly from Prisma (e.g. "the last post from the user" or "whether one can delete this user"); what's the best way to extract and reuse the logic for these fields?
- How to mask output types (e.g. make sure that I don't return User.passwordHash
ever)
- How to express calculated fields (e.g. fullName
= firstName
+ lastName
)
- How to reuse output types in nested components (e.g. components that don't directly useQuery
)
Thanks 🙏3 Replies
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
You can also do a middleware for that second one to make sure the result never returns any blocklisted keys
also looks like your'e looking for prisma validators https://www.prisma.io/docs/concepts/components/prisma-client/advanced-type-safety/prisma-validator
here's how ot infer https://github.com/trpc/examples-next-prisma-starter/blob/fdb1167269d3ffc5f437663639ad587679590edb/src/pages/post/%5Bid%5D.tsx#L6
example of a prisma validator https://github.com/trpc/examples-next-prisma-starter/blob/fdb1167269d3ffc5f437663639ad587679590edb/src/server/routers/post.ts#L11-L22
calculated fields i usually do with some like reused
mapToUser()
-style pure functions
but prisma is soon releasing prisma client extensions to help with thatGotcha thanks