You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在DTO中使用自定义类时遭遇GraphQL输入类型识别错误求助

问题解决:Cannot Determine a graphQL inputType for the "geoData"

报错核心原因:你在标记为输入类型(@InputType)的LocatorCreateDto中,使用了标记为输出类型(@ObjectType)的GeoData作为字段类型。GraphQL严格区分输入、输出类型,@ObjectType仅用于定义查询返回的输出结构,不能作为输入参数的类型。

两种解决办法:

办法1:将GeoData改为纯输入类型

如果GeoData只用于输入场景,直接把@ObjectType替换为@InputType即可:

@InputType()
export class GeoData {
    @Field((type) => String)
    type: string

    @Field(() => String)
    properties: string
    
}

@InputType()
export class LocatorCreateDto {
    @IsNotEmpty()
    @Field(() => String)
    name: string;
    
    @IsNotEmpty()
    @Field(() => String)
    lastName: string
    
    @IsNotEmpty()
    @ValidateNested({ each: true})
    @Type(() => GeoData)
    @Field((type) => GeoData)
    geoData: GeoData
    
}

办法2:让GeoData同时支持输入和输出类型

如果GeoData还需要作为查询返回的输出类型,可以同时添加@ObjectType和@InputType装饰器:

@ObjectType()
@InputType()
export class GeoData {
    @Field((type) => String)
    type: string

    @Field(() => String)
    properties: string
    
}

@InputType()
export class LocatorCreateDto {
    @IsNotEmpty()
    @Field(() => String)
    name: string;
    
    @IsNotEmpty()
    @Field(() => String)
    lastName: string
    
    @IsNotEmpty()
    @ValidateNested({ each: true})
    @Type(() => GeoData)
    @Field((type) => GeoData)
    geoData: GeoData
    
}

额外说明

  • @ValidateNested和@Type是class-validator的校验装饰器,这部分逻辑没问题,无需修改。
  • GraphQL的输入、输出类型是独立概念,即便结构完全一致,也必须用对应装饰器标记,不能混用。

内容的提问来源于stack exchange,提问作者Yaniv Suriyano

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 19:45:50