Ofer
04/23/2021, 12:37 PMmodel Product {
id Int @id @default(autoincrement())
cars Car[]
boats Boat[]
planes. Plane[]
}
model Car {
id Int @id @default(autoincrement())
product Product @relation(fields: [productId], references: [id])
productId Int
}
model Boat {
id Int @id @default(autoincrement())
product Product @relation(fields: [productId], references: [id])
productId Int
}
model Plane {
id Int @id @default(autoincrement())
product Product @relation(fields: [productId], references: [id])
productId Int
}
But it doesn’t feel intuitive to me.
Is this the correct way in this case?Robert Witchell
04/25/2021, 2:32 AMRobert Witchell
04/25/2021, 2:41 AMmodel Product {
id Int @id @default(autoincrement())
carId Int
car Car
boatId Int
boat Boat
planeId Int
plane Plane
}
and then in your model you could have:
const Product = objectType({
name: 'Product',
definition(t) {
t.model.id()
t.model.car()
t.model.carId()
t.model.boat()
t.model.boatId()
t.model.plane()
t.model.planeId()
t.field.productType({
resolve: (parent, args, context, info) => {
if (parent.carId) {
return PRODUCT_ENUM_CAR
}
// etc
},
})
t.field.getProductType({/*more here*/})
},
})Ofer
04/25/2021, 5:25 AMMark
04/25/2021, 7:06 PMmodel Product {
id Int @id @default(autoincrement())
car Car?
boat Boat?
plane Plane?
}
model Car {
id Int @id @default(autoincrement())
product Product @relation(fields: [productId], references: [id])
productId Int @unique
}
model Boat {
id Int @id @default(autoincrement())
product Product @relation(fields: [productId], references: [id])
productId Int @unique
}
model Plane {
id Int @id @default(autoincrement())
product Product @relation(fields: [productId], references: [id])
productId Int @unique
}Mark
04/25/2021, 7:12 PMproductType enum field to the model Product , and make an object type like this:
const product = objectType({
name: 'Product',
definition(t) {
t.int('id')
t.field('productType', { type: 'ProductTypeEnum' })
t.field('details', {
type: 'DetailsUnionType',
resolve(root, _args, { prisma }) {
if (root.productType === 'CAR') {
return prisma.product.findUnique({ where: { id: root.id }}).car()
} else ...
}
})
}
})
🤷Robert Witchell
04/26/2021, 1:21 AMresolve(root, _args, { prisma }) {
return prisma[productType].findUnique({ where: { id: root.id }}).{productType}()
}Mark
04/26/2021, 1:37 AMprisma[root.productType].findUnique({ where: { productId: root.id } })