Search code examples
graphqlgraphql-jsapollo-servertypegraphql

Define an arg type using just the typename in typegraphql


We have a factory which creates types using generic types:

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

  @GqlInputType(`${props.model.name}CreateInput`)
  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;
}

For now we have to declare the input args like: Arg('input') input: UpdateInput

As my types are generated using a factory the classes won't be available at (not runtime?). Is there a way to refer to those dynamically generated inputs? Is there a way to do something like: Arg('input') input: resolveType('ProductCreateInput')


Solution

  • Assuming that resolveType returns a class decorated with @InputType and @Fields:

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

    You just need to provide a runtime value as the second parameter of @Arg decorator