Zion
Zion9mo ago

Help with inferring output

return ctx.prisma.product.findMany({
include: {
price: {
select: {
contractor: group === "CONTRACTOR",
customer: group === "CUSTOMER",
frequent: group === "FREQUENT",
professional: group === "PROFESSIONAL",
vip: group === "VIP",
visitor: group === "VISITOR",
},
},
},
});
return ctx.prisma.product.findMany({
include: {
price: {
select: {
contractor: group === "CONTRACTOR",
customer: group === "CUSTOMER",
frequent: group === "FREQUENT",
professional: group === "PROFESSIONAL",
vip: group === "VIP",
visitor: group === "VISITOR",
},
},
},
});
I have the following return and I want to infer the types of a single item, before I added the include bit the code worked, but now it gives me an error, how to set it up correctly?
No description
5 Replies
Zion
Zion9mo ago
No description
Fouad Raheb
Fouad Raheb9mo ago
Prisma won't properly infer types with a condition in select input You better create a separate prisma findMany for each group type
Zion
Zion9mo ago
I thought this might be the problem but even if I just have a price: true the error is the same, I dont need it to tell me what pricing is there, I just dont want it sent to the user, it pretty much just have every price as number | undefined which is fine by me the issue is that I need to turn it to a singular item from the array and its not working when having that include, with or without the coditions
Tamás Soós
Tamás Soós9mo ago
Does this help? Prisma has some tools to infer the types of select, include, etc..., I also had a similar use case.
Prisma
Advanced type safety (Reference)
Prisma Client provides full type safety for queries, even for partial queries or included relations. This page explains how to leverage the generated types and utilities.
Tamás Soós
Tamás Soós9mo ago
Sorry, forgot to address the conditionals. I found a bug report here
GitHub
Conditional select and include not inferring expected type · Issue...
Bug description Our backend API has routes where we can optionally perform what the include API does. For example fetch user and optionally include posts written by this user. I expect the inferred...