仅使用 typegraphql 中的类型名定义 arg 类型

Posted

技术标签:

【中文标题】仅使用 typegraphql 中的类型名定义 arg 类型【英文标题】:Define an arg type using just the typename in typegraphql 【发布时间】:2020-05-08 22:31:31 【问题描述】:

我们有一个使用泛型类型创建类型的工厂:

export function GenericResolverFactory(props: FactoryProps) 
  @GqlType(`$props.model.nameResponse`)
  class ModelPaginatedResponse extends ResponseFactory(props.modelClass) 
    // you can add more fields here if you need
  

  @GqlInputType(`$props.model.nameCreateInput`)
  class CreateInput extends CreateInputFactory(props.createInput)  

  @Resolver(_of => props.modelClass,  isAbstract: true )
  abstract class GenericResolver 
    @Mutation(_returns => props.modelClass,  name: `create$startCaseName` )
    create(@Arg('data') data: CreateInput, @Ctx() _context: UserContext) 
      return this.__getService().create(data);
    
  

  return ModelResolverClass;

现在我们必须声明输入参数,例如:Arg('input') input: UpdateInput

由于我的类型是使用工厂生成的,因此类在(不是运行时?)时不可用。有没有办法引用那些动态生成的输入?有没有办法做类似的事情:Arg('input') input: resolveType('ProductCreateInput')

【问题讨论】:

【参考方案1】:

假设resolveType返回一个用@InputType@Fields装饰的类:

@Arg('input', type => resolveType('ProductCreateInput')) input: ICreateInput`

你只需要提供一个运行时值作为@Arg装饰器的第二个参数

【讨论】:

以上是关于仅使用 typegraphql 中的类型名定义 arg 类型的主要内容,如果未能解决你的问题,请参考以下文章

有没有办法使用 TypeGraphQL 定义 GraphQL 片段?

TypeGraphql 和 Prisma 2 类型冲突

Passport (oAuth2) 如何与 GraphQL (TypeGraphQL) 一起使用?

使用 TypeGraphQL 在浏览器中为 Apollo 本地状态生成 typedef 和解析器

接口仅用来定义类型

如何使用 typegraphql-prisma 保护用户数据