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

Unable to control `{nullable: true}` of generated fields.

Open Trottero opened this issue 1 year ago • 3 comments
trafficstars

Hi! thanks for building the generator, it has saved me so much time already!

I am however running into an issue where I can't seem to configure one-to-many relations correctly, specifically, I can't seem to control the nullability of the array type.

For example;

model User {
  id String @id
  posts BlogPost[]
}

model BlogPost {
  id String @id
  userId String
  user User @relation(fields: [userId], references: [id])
}

Generates (just included the user class)

@ObjectType()
export class User {
    @Field(() => ID, {nullable:false})
    id!: string;
   
    @Field(() => [BlogPost], {nullable:true}) <-- it's not nullable, its always an array
    posts?: Array<BlogPost>
}

This makes it so the gql server thinks posts is nullable, when in reality it always returns an array, with it being empty if there are no posts.

How do I control this behaviour?

Trottero avatar Dec 10 '23 15:12 Trottero

Yeah, totally agree that current behaviour of lists being optional is incorrect, default list field type mapping should definitely be like [Type!]!.

vladyslav-n avatar Jan 25 '24 22:01 vladyslav-n

Yeah, totally agree that current behaviour of lists being optional is incorrect, default list field type mapping should definitely be like [Type!]!.

I dont't mind the default being nullable though, I'd rather focus on being able to configure it as other setups might return null anyway.

Trottero avatar Feb 01 '24 08:02 Trottero

please any solution to this yet

SOG-web avatar Aug 29 '24 23:08 SOG-web