Snigdha Singh
Snigdha Singh

Reputation: 108

Validation of args passed to generic mutation in base resolver

Current behavior

Base resolver:

export function BaseResolver<
  T extends Type<BaseClass>,
  createClassDto extends Type<CreateBaseClassDto>,
>(classRef: T, createClassArgsDtoRef: createClassDto): any

Derived resolver:

@Resolver((of) => DerivedXClass)
export class DerivedXResolver extends BaseResolver(
  DerivedXClass,
  CreateDerivedXDto,
) 
 async createMutation(
      @Args({ type: () => createClassArgsDtoRef })
      args: Type<CreateBaseClassDto>,
    )

Minimum reproduction code

It has the steps to reproduce and the expected behaviour.

https://github.com/snigdha920/nestjs-validation-args

Upvotes: 1

Views: 266

Answers (1)

Jay McDoniel
Jay McDoniel

Reputation: 70191

I'm sure there's probably a cleaner way to do this, but something hacky would look like

    @Mutation(() => Boolean, { name: `create${classRef.name}` })
    async createMutation(
      @Args({ type: () => createClassArgsDtoRef })
      args: // @ts-ignore
      createClassArgsDtoRef,
    ) {
      console.log('class is ', classRef.name);
      console.log('args captured are ', args);
      return true;
    }
  }

Typescript essentially doesn't understand that what you pass here is in fact a class reference and it should be used as a type. There's probably a better way to set this, maybe even the use of a custom parameter decorator, but this works for now and satisfies general type usage to the best of my knowledge

Upvotes: 1

Related Questions