使用@ArgsType的GraphQL参数类型无法识别的解决方案咨询
问题:typegraphql-prisma自动生成的@ArgsType参数引发CannotDetermineGraphQLTypeError错误
使用typegraphql-prisma自动生成的、带有@ArgsType装饰器的FindFirstCompanyTalentRosterArgs作为GraphQL查询参数时,触发CannotDetermineGraphQLTypeError,提示无法确定参数的GraphQL输入类型。手动将生成文件中的装饰器改为@InputType可解决问题,但希望不修改自动生成的代码。
相关代码
@Query(() => CompanyTalentRoster) async findCompanyTalentRoster( @Args() args: FindFirstCompanyTalentRosterArgs ): Promise<CompanyTalentRoster | null> { const data: Prisma.companyTalentRosterFindFirstArgs = { where: { ...args.where, // TODO: companyId: ctx.companies[0].id, }, orderBy: args.orderBy, }; return this.companyTalentRosterService.findFirst(data); }
错误信息
[2023-03-14T17:04:41.044] [ERROR] [TSED] - CannotDetermineGraphQLTypeError: Cannot determine GraphQL input type for argument named 'args' of 'findCompanyTalentRosters' of 'CompanyTalentRosterResolver' class. Is the value, that is used as its TS type or explicit type, decorated with a proper decorator or is it a proper input value? at Function.getGraphQLInputType (/Users/backend/node_modules/type- graphql/dist/schema/schema-generator.js:560:19)
解决方法
1. 调整typegraphql-prisma生成配置(推荐)
修改typegraphql-prisma的生成配置文件(通常是prisma/typegraphql.ts),指定生成@InputType而非@ArgsType,自动生成的代码会直接使用正确的装饰器:
import { generatorHandler } from "@prisma/generator-helper"; import { generate } from "typegraphql-prisma/generator"; generatorHandler({ onManifest() { return { defaultOutput: "../generated/typegraphql-prisma", prettyName: "TypeGraphQL Prisma Generator", }; }, onGenerate(options) { generate({ ...options, // 配置生成InputType装饰器 inputTypeDecorator: "InputType", }); }, });
修改后重新执行prisma generate,生成的参数类会自动适配需求,无需手动修改。
2. 自定义包装InputType类
如果不想修改生成配置,可以创建一个自定义的InputType类继承自动生成的参数类,并用@InputType装饰:
import { InputType } from "type-graphql"; import { FindFirstCompanyTalentRosterArgs } from "../generated/typegraphql-prisma"; @InputType() export class CustomFindFirstCompanyTalentRosterArgs extends FindFirstCompanyTalentRosterArgs {}
然后在Resolver中使用这个自定义类替代原生成类:
@Query(() => CompanyTalentRoster) async findCompanyTalentRoster( @Args() args: CustomFindFirstCompanyTalentRosterArgs ): Promise<CompanyTalentRoster | null> { const data: Prisma.companyTalentRosterFindFirstArgs = { where: { ...args.where, // TODO: companyId: ctx.companies[0].id, }, orderBy: args.orderBy, }; return this.companyTalentRosterService.findFirst(data); }
内容的提问来源于stack exchange,提问作者Daniel
相关产品推荐
相关产品推荐

