prisma-nestjs-graphql icon indicating copy to clipboard operation
prisma-nestjs-graphql copied to clipboard

Optional fields are not optional in the generated TS class

Open kasir-barati opened this issue 1 year ago • 2 comments

Hey guys,

I have seen issue https://github.com/unlight/prisma-nestjs-graphql/issues/41 but I have a different case, I have an optional field defined in my model:

model AlertType {
  id                  String    @id @default(uuid())
  name           String    @unique @db.VarChar(200)
  description String?   @db.VarChar(500)
  // ...
}

And it is generating the following model for it:

// ...
@ObjectType()
export class Alert {
    @Field(() => ID, {nullable:false})
    id!: string;

    @Field(() => String, {nullable:false})
    title!: string;

    @Field(() => String, {nullable:true})
    description!: string | null;

    // ...
}

I am running into the same issue in my code. Do you think I am missing something? Here is my repo: https://github.com/kasir-barati/graphql/tree/main/apps/botprobe-nest

To reproduce:

  1. pnpm i.
  2. nx prisma-generate botprobe-nest.
  3. Then open apps/botprobe-nest/src/@generated/alert/alert.model.

Anything I can do to make it optional like what happens to the relation fields?

kasir-barati avatar Dec 03 '24 06:12 kasir-barati

BTW This did not cause me any trouble in runtime but still it is kinda annoying. Although I am not sure if this is such a bad thing.

kasir-barati avatar Dec 03 '24 14:12 kasir-barati

Field description is nullable from graphql perspective. Not from the typescript. If you will make ts field optional by 'question mark' modifier this field will be treated as possible undefined. But prisma doesnt return undefined for optional field if it is selected. It returns null.

I saw prisma discussions about this, people want receive undefined instead of null, but idk the result...

unlight avatar Jan 26 '25 02:01 unlight