Jakub Kylián
Jakub Kylián

Reputation: 319

NestJS + GraphQL - optional data in @InputType (code first)

I would like to make some data I send in graphql mutation optional but not nullable. I'm using the code-first approach.

For example:

@InputType()
export class CreateUserDto {
  @Field()
  firstName?: string;
}

I understand I can use the nullable parameter or possibly defaultValue but none of these options are really usable in my case.

Also, I know there is the Nest's PartialType utility but that would mean creating another file with the class if I'm correct and that doesn't seem very efficient.

Upvotes: 5

Views: 5449

Answers (2)

Khalid Al Yafey
Khalid Al Yafey

Reputation: 61

set nullable to true in @Field decorator like:

@Field({ nullable: true })

So your code should be:

@InputType()
export class CreateUserDto {
  @Field({ nullable: true })
  firstName?: string;
}

Upvotes: 6

Bekzod Sanakulov
Bekzod Sanakulov

Reputation: 1

use isOptional() validators

This will help you.

Upvotes: -1

Related Questions