[英]Error: Cannot determine a GraphQL input type, while using Nestjs + Graphql + Typeorm
[英]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.