How to express an union type in a GraphQL InputType

1.6k views Asked by At

In a NestJS GraphQL application, I am trying to express a union type for an InputType field.

My issue: At build, there is an error: Cannot determine a GraphQL input type for the "config". Make sure your class is decorated with an appropriate decorator.

Here is my code


// Object type 1
@InputType()
class FontDto {
  @Field()
  name: string

  @Field()
  file: string
}

// Object type 2
@InputType()
class PictoDto {
  @Field()
  name: string

  @Field()
  file: string
  
  @Field()
  variant: string
}

// I create my union here
const ConfigDtoUnion = createUnionType({
  name: 'ConfigDto',
  types: () => [FontDto, PictoDto],
});

// And use it in the config field.
@InputType()
class SlotDto implements ISlot {
  @Field()
  name: string;
  
  @Field()
  type: string;

  @Field(type => ConfigDtoUnion)
  config: FontDto | PictoDto;
}

// My entire DTO
@InputType()
export class CreateProductDto implements IProduct {
  @Field(() => [SlotDto])
  @IsArray()
  @Type(() => SlotDto)
  slots: SlotDto[]
}
0

There are 0 answers