简体   繁体   English

NestJS + GraphQL - @InputType 中的可选数据(代码优先)

[英]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.

相关问题 使用代码优先方法在 NestJS 中使用 GraphQL 模型出错 - Getting error with GraphQL model in NestJS, using code-first approach GraphQL 架构未使用 NestJS 更新(代码优先方法) - GraphQL Schema not updated with NestJS (code-first approach) NestJs Graphql(CLI 插件代码优先)+ Mongoose 无法解析“../../../../mongoose” - NestJs Graphql (Code first with CLI Plugin)+ Mongoose cannot resolve "../../../../mongoose" 在 NestJS 中实现 GraphQL 全局 Object 识别(代码优先方法) - Implementing GraphQL Global Object Identification in NestJS (code-first approach) NestJS Schema 先 GraphQL 序列化 - NestJS Schema First GraphQL Serialization NestJS:在使用 GraphQL 和 MongoDB 定义 InputType 和 ObjectType 字段时,我们可以使用自定义类型和类吗 - NestJS: Can we use custom Type and Classes when defining InputType and ObjectType Field with GraphQL & MongoDB 使用 NestJS TestingModule、GraphQL 代码优先和 TypeOrm 进行 e2e 测试的问题 - Problem with e2e testing with NestJS TestingModule, GraphQL code first and TypeOrm 使用自定义字段在nestjs graphql 代码优先方法中定义实体导致错误 - Defining entities in nestjs graphql code first approach with custom field results in error NestJS + GraphQL 联合和模式优先 GraphQLDefinitionsFactory - NestJS + GraphQL Federation and schema first GraphQLDefinitionsFactory nestjs graphql - 保留数据并将其用于其他请求 - nestjs graphql - keep data and use it for other request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM