Trujillo
WWasp
•Created by Trujillo on 2/28/2025 in #đŸ™‹questions
How to properly type Prisma query results with included relations in Wasp?
Hi everyone, I'm trying out Wasp, and I have a small problem I would like share here.
model Product {
id String @id @default(uuid())
name String
description String
price Float
stockQuantity Int @default(0)
unit String
// Remove the files relation as it will be accessed from the other side
file File?
user User @relation(fields: [userId], references: [id])
userId String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model File {
id String @id @default(uuid())
createdAt DateTime @default(now())
// Make this a one-to-one relation
product Product @relation(fields: [productId], references: [id], onDelete: Cascade)
productId String @unique // Add unique constraint to ensure one-to-one relationship
name String
type String
key String
uploadUrl String
}
I have this two models, then, in one my querys, I want to get Products with Files, so I do:
export const getProducts: GetProducts<void, Product[]> = async (args, context) => {
if (!context.user) {
throw new HttpError(401)
}
return context.entities.Product.findMany({
where: { user: { id: context.user.id }},
orderBy: { createdAt: 'asc'},
include: {
file: true
}
})
};
This works great and the return object brings Product with files, however, the type definition doesn't include the file property, so I get an error in the frontend when I want to use product[n].file.
How can I fix this?11 replies