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

Optional fields set as required in model

Open SynergyEvolved opened this issue 4 years ago • 3 comments
trafficstars

Hi there,

I am not sure what I am missing but when I put a field as optional in the schema file like:

    description        String?

end up with the following in the generated model:

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

I can see that it is setting nullable to true, but it is also making the optional description field required with a ! which is not what I want.

Thanks for your help.

-Paul

SynergyEvolved avatar Sep 25 '21 22:09 SynergyEvolved

Optional fields made as string | null for compatibility with prisma model types, check this issue #41

unlight avatar Sep 25 '21 22:09 unlight

Sorry I do not understand. I delete the files before regeneration and I am using the latest version (14.2.1).

The schema has a ? and the model file as has !

Why does it have a "!" ?

SynergyEvolved avatar Sep 26 '21 06:09 SynergyEvolved

@SynergyEvolved It is not about deleting, it's about compatibility. Exclamation mark in typescript means "suppress errors when accessing to possible nullable or undefined" Without exclamation mark with compilerOptions.strict = true it will give error Property '' has no initializer Another way to avoid exclamation mark is to initialize property:

description: string | null = null;

Making type as string | undefined will give error when assigning prisma type to graph model and vice versa:

Types of property '' are incompatible.
    Type 'string | null' is not assignable to type 'string | undefined'.
      Type 'null' is not assignable to type 'string | undefined'.

Making type as string | null | undefined will give error of assigning graphql model to prisma type.

  Types of property '' are incompatible.
    Type 'string | null | undefined' is not assignable to type 'string | null'.
      Type 'undefined' is not assignable to type 'string | null'.

unlight avatar Sep 26 '21 09:09 unlight