NestJS GraphQL报错:无法确定expectedInvestmentSupportInfo的输出类型
问题
我创建了名为UploadedInvestmentDocumentInput的DTO,并关联到expectedInvestmentSupportInfo属性,但报错:
uncaughtException: Cannot determine a GraphQL output type for the "expectedInvestmentSupportInfo"
注释opportunity-account.output.ts中的expectedInvestmentSupportInfo字段后错误消失,请问该字段缺少什么装饰器或属性?
opportunity-account.input.ts
import { UploadedDocumentInput } from '@app/dto/common.input'; import { Field, InputType, OmitType, PickType, registerEnumType, } from '@nestjs/graphql'; import { Type } from 'class-transformer'; import { IsEnum, ValidateNested } from 'class-validator'; import { ExpectedInvestmentSupportInfoType, SourceOfFundsType, } from '@/generated-cbms-api-client'; import { OpportunityAccount } from './opportunity-account.response'; registerEnumType(SourceOfFundsType, { name: 'SourceOfFundsType' }); registerEnumType(ExpectedInvestmentSupportInfoType, { name: 'ExpectedInvestmentSupportInfoType' }); @InputType() export class UploadedInvestmentDocumentInput extends OmitType(UploadedDocumentInput, ['documentType'], InputType) { @IsEnum(ExpectedInvestmentSupportInfoType) @Field(() => ExpectedInvestmentSupportInfoType) documentType: ExpectedInvestmentSupportInfoType; } @InputType() export class UpsertAnticipatedInvestmentActivitiesInput extends PickType( OpportunityAccount, [ 'isPlanningInvestIn12Months', 'anticipatedInvestmentOpportunitiesCountRange', 'investmentDescription', ], InputType, ) { @ValidateNested({ each: true }) @Type(() => UploadedInvestmentDocumentInput) @Field(() => [UploadedInvestmentDocumentInput], { description: 'Expected investment support info' }) expectedInvestmentSupportInfo: UploadedInvestmentDocumentInput[]; }
UploadedDocumentInput(公共DTO)
import { Field, InputType } from '@nestjs/graphql'; import { FileUpload, GraphQLUpload } from 'graphql-upload'; @InputType() export class UploadedDocumentInput { @Field(() => Number) documentType: number; @Field(() => GraphQLUpload) frontSideDoc: FileUpload; @Field(() => GraphQLUpload, { nullable: true }) backSideDoc?: FileUpload; }
opportunity-account.output.ts
class UploadedInvestmentDocumentResponse extends OmitType(UploadedDocumentResponse, ['documentType'], InputType) { @IsEnum(ExpectedInvestmentSupportInfoType) @Field(() => ExpectedInvestmentSupportInfoType) documentType: ExpectedInvestmentSupportInfoType; } @ObjectType() export class OpportunityAccount extends AccountsCommonDetails { @IsMongoId() @Field(() => String, { description: 'Customer id' }) customerId: string; // 我认为问题出在这里(注释此字段后错误消失!) @IsEnum(UploadedInvestmentDocumentResponse) @ValidateNested({ each: true }) @Type(() => UploadedInvestmentDocumentResponse) @Field(() => [UploadedInvestmentDocumentResponse], { description: 'Expected investment support info' }) expectedInvestmentSupportInfo: UploadedInvestmentDocumentResponse[]; }
问题根源及修复方案
核心问题
UploadedInvestmentDocumentResponse未标记为GraphQL输出类型
该类是用于GraphQL响应的DTO,但既没有添加@ObjectType()装饰器,还错误地在OmitType中传入了InputType作为类型参数——GraphQL无法识别未标记输出类型的类作为字段类型。错误使用
@IsEnum验证器expectedInvestmentSupportInfo是对象数组,而非枚举值,你给它添加的@IsEnum(UploadedInvestmentDocumentResponse)完全无效,@IsEnum仅能用于枚举类型的字段验证。
修复后的opportunity-account.output.ts代码
import { ObjectType, OmitType, Field } from '@nestjs/graphql'; import { ValidateNested } from 'class-validator'; import { Type } from 'class-transformer'; import { ExpectedInvestmentSupportInfoType } from '@/generated-cbms-api-client'; import { UploadedDocumentResponse } from './path-to-uploaded-document-response'; // 确保导入正确的父类 // 标记为GraphQL输出类型,同时修正OmitType的类型参数 @ObjectType() export class UploadedInvestmentDocumentResponse extends OmitType(UploadedDocumentResponse, ['documentType'], ObjectType) { @Field(() => ExpectedInvestmentSupportInfoType) documentType: ExpectedInvestmentSupportInfoType; } @ObjectType() export class OpportunityAccount extends AccountsCommonDetails { @Field(() => String, { description: 'Customer id' }) customerId: string; // 移除错误的@IsEnum,保留正确的嵌套验证和类型转换 @ValidateNested({ each: true }) @Type(() => UploadedInvestmentDocumentResponse) @Field(() => [UploadedInvestmentDocumentResponse], { description: 'Expected investment support info' }) expectedInvestmentSupportInfo: UploadedInvestmentDocumentResponse[]; }
关键修复点总结
- 所有用于GraphQL响应的类必须添加
@ObjectType()装饰器,使用OmitType/PickType生成输出类型时,第三个参数要传ObjectType。 - 仅在枚举类型字段上使用
@IsEnum验证器,对象/数组类型字段不要误用。
内容的提问来源于stack exchange,提问作者Abdullah Ch
相关产品推荐
相关产品推荐

