繁体   English   中英

是否可以使用 nestjs/graphql Args 维护平面输入结构?

[英]Is it possible to maintain flat input structure with nestjs/graphql Args?

我有一个简单的查询:

@Query(() => ProfileOutput)
async profile(@Args('id') id: string) {
  return await this.profileFacade.findProfileById(input.id);
}

问题是我想在这里应用class-validator中的@IsMongoId()作为id 我不想在这里创建新的@InputType ,因为我不想更改 API 规范。 有没有办法在这里应用像@IsMongoId这样的验证器,而无需更改前端的查询定义?

对于任何寻求答案的人,我发现了一个名为专用参数 class的功能。 而不是像我这样想的那样创建新的输入类型:

@InputType()
export class MongoIdBaseInput {
  @IsMongoId()
  @Field()
  id: string;
}

@Query(() => ProfileOutput)
async profile(@Args('data') input: MongoIdBaseInput) {
  return await this.profileFacade.findProfileById(input.id);
}

我们可以几乎相同地定义它,但是使用ArgsType注释输入它将为我们维护 args 的平面结构:

@ArgsType()
export class MongoIdBaseInput {
  @IsMongoId()
  @Field()
  id: string;
}

@Query(() => ProfileOutput)
async profile(@Args() input: MongoIdBaseInput) {
  return await this.profileFacade.findProfileById(input.id);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM