Prisma generate object fields options #583
-
Hi, I've created a small Prisma generator that kind of mimick 'nexus-prisma' but for Pothos, As I like the mix between needing to explicitly declare your every needed objects, fields, etc in your schema but most of the underlying information is generated from the prisma schema. The only problem I'm currently facing is the typings which are pretty complexed. Not sure if it would be actually possible. Here's an example of what the generator does. The following schema /// The user
model User {
id String @id @default(cuid())
email String @unique
username String @unique
/// The password
password String
} Will generate the following TS export const User = {
$name: "User",
$description: "The user",
id: {
name: "id",
type: "ID", // <-- These is what seems to cause the typing issue since type is typed as `string`
description: undefined,
nullable: false,
},
email: {
name: "email",
type: "String",
description: undefined,
nullable: false,
},
username: {
name: "username",
type: "String",
description: undefined,
nullable: false,
},
password: {
name: "password",
type: "String",
description: "The password",
nullable: false,
},
} Importing this generated TS into the Primsa object import { builder } from './builder'
import { User } from './generated/user'
// Get typing errors as `type` is typed as `string`
builder.prismaObject('User', {
name: User.$name,
description: User.$description,
fields: (t) => ({
id: t.expose('id', { ...User.id }),
email: t.expose('email', { ...User.email }),
username: t.expose('username', { ...User.username }),
password: t.expose('password', { ...User.password })
}),
}); The workaround for now is to explictly pass the // No typing errors as type overwrite the type `string` with the correct type
builder.prismaObject('User', {
name: User.$name,
description: User.$description,
fields: (t) => ({
id: t.expose('id', { ...User.id, type: "ID" }),
email: t.expose('email', { ...User.email, type: "String" }),
username: t.expose('username', { ...User.username, type: "String" }),
password: t.expose('password', { ...User.password, type: "String" })
}),
}); So the question is basically to know if it's possible to get the typing right without the need to apply the workaround for each field? If it is, is there an example of something similar so that I canget a better idea how to achieve this? Thanks for your time, really appreciate! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
If you have your generator add |
Beta Was this translation helpful? Give feedback.
If you have your generator add
as const
to your definitions that might fix your type issues without having to generate more complicated types