[英]NestJS + GraphQL - optional data in @InputType (code first)
I would like to make some data I send in graphql mutation optional but not nullable.我想让我在 graphql 突变中发送的一些数据是可选的,但不能为空。 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.我知道我可以使用可为
nullable
的参数或可能使用defaultValue
,但这些选项在我的情况下都没有真正可用。
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.另外,我知道有 Nest 的
PartialType
实用程序,但如果我是正确的,那将意味着使用 class 创建另一个文件,这似乎不是很有效。
use isOptional() validators
This will help you.这会帮助你。
set nullable to true in @Field decorator like:在 @Field 装饰器中将 nullable 设置为 true,例如:
@Field({ nullable: true }) @Field({ 可为空:真 })
So your code should be:所以你的代码应该是:
@InputType()
export class CreateUserDto {
@Field({ nullable: true })
firstName?: string;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.