Object relation doesn't compile to TypeScript
I have an explicit many-to-many relation that I want to display data. After migrating from version 0.11 to version 0.12, I can't find a way to display objects of my entity. Here is my schema:
entity Material {=psl
id Int @id @default(autoincrement())
code String @unique
name String
count Int
measurementUnit String
products ProductMaterials[]
psl=}
entity ProductMaterials {=psl
product Product @relation(fields: [productId], references: [id])
productId Int
material Material @relation(fields: [materialId], references: [id])
materialId Int
materialCount Int
measurementUnit String
@@id([productId, materialId])
psl=}
entity Product {=psl
id Int @id @default(autoincrement())
code String @unique
name String
description String
materials ProductMaterials[]
productionPlans ProductionPlanProducts[]
psl=}
When i try to display products for material error occurs: Property 'products' does not exist on type 'GetResult<{ id: number; code: string; name: string; count: number; measurementUnit: string; }, unknown> & {}'.
Since I have migrated from an old version of Wasp, in which my code works, I'm curious about what makes the difference that in the previous version, the import from @wasp/entities
includes objects of entities, and in the new version, the import from wasp/entities
doesn't include them.
I have found a previous post and solution which includes creating a new type. Is there any way to include objects with types from prisma/entities
?8 Replies
Hm @david.fejes this is quite peculiar! We did quite same changes in 0.12, but I don't think we introduced a regression like this on purpose, if that is indeed what it is.
@miho and @Filip will know more about this specific feature so I am pinging them!
When i try to display products for material error occurs: Property 'products' does not exist on type 'GetResult<{ id: number; code: string; name: string; count: number; measurementUnit: string; }, unknown> & {}'.Howdy π Could you please share your actions / queries code? Only the Prisma related parts if you don't want to share everything.
I have found a previous post and solution which includes creating a new type. Is there any way to include objects with types from prisma/entities?Could you please link the post?
Since im overriding CRUD i have created function in tasks.js
export const getMaterials: Materials.GetAllQuery<void, Material[]> = async (args, context) => {
const { Material } = context.entities;
return await Material.findMany({
include: {
products: {
include: {
product: true,
},
},
},
});
};
Here is github link: https://github.com/fejes99/MaterialPlanningApp/blob/main/src/materials/operations/tasks.tsGitHub
MaterialPlanningApp/src/materials/operations/tasks.ts at main Β· fej...
Contribute to fejes99/MaterialPlanningApp development by creating an account on GitHub.
Wohooo @david.fejes, you just became a Waspeteer level 1!
Here is a link to the post: https://discord.com/channels/686873244791210014/1186410195732140073
So this is an issue with full-stack type safety here π
We use the type you annotated the function with to give you types on the client.
-> In your case it was:
Materials.GetAllQuery<void, Material[]>
-> This says: This function will return a list of Material
(the Material
type has only its types and not the relationships)
You need to annotate it more precisely since you are not returning only a list of Material
but also including the relations which change the type.
π The way to do this would be to do:
You see, I'm now combing the basic Material
type with the ProductMaterials
and Product
at places where we have the included relationships.
I know this is annoying! The better behaviour would be to automatically infer the right type and not make you define this complex type.
I've noted this future improvement in this issue: https://github.com/wasp-lang/wasp/issues/1884Thanks!