简体   繁体   English

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

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

I have a simple query:我有一个简单的查询:

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

The problem is that I want to apply @IsMongoId() from class-validator for the id here.问题是我想在这里应用class-validator中的@IsMongoId()作为id I do not want to create new @InputType here, because I do not want to change API specification.我不想在这里创建新的@InputType ,因为我不想更改 API 规范。 Is there a way to apply validator like @IsMongoId here without the need to change query definition for frontend?有没有办法在这里应用像@IsMongoId这样的验证器,而无需更改前端的查询定义?

For anyone seeking an answer I found a feature called dedicated argument class .对于任何寻求答案的人,我发现了一个名为专用参数 class的功能。 Instead of creating new input type as I thought like this:而不是像我这样想的那样创建新的输入类型:

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

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

We can define it almost the same, but annotate input with ArgsType it will maintain flat structure of args for us:我们可以几乎相同地定义它,但是使用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