簡體   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