prisma-nestjs-graphql icon indicating copy to clipboard operation
prisma-nestjs-graphql copied to clipboard

Destructured enum

Open magrinj opened this issue 2 years ago • 1 comments

Is there a reason why enums are destructured in the ObjectType ?

export enum MediaType {
    MOVIE = "MOVIE",
    TV = "TV"
}


registerEnumType(MediaType, { name: 'MediaType', description: undefined })

@ObjectType()
export class MovieGroupBy {
    @Field(() => MediaType, {nullable:false})
    mediaType!: keyof typeof MediaType; // <----- The actually generated file
}

instead of

export enum MediaType {
    MOVIE = "MOVIE",
    TV = "TV"
}


registerEnumType(MediaType, { name: 'MediaType', description: undefined })

@ObjectType()
export class MovieGroupBy {
    @Field(() => MediaType, {nullable:false})
    mediaType!: MediaType; // <----- What I'm expecting
}

Doing that lead to type errors when using the enum with a dynamic value and a type a proper type.

Official NestJS documentation doens't destruct the enums: https://docs.nestjs.com/graphql/unions-and-enums

magrinj avatar Jul 12 '22 10:07 magrinj

Mostly because of TS error below:

export enum MediaType {
    MOVIE = "MOVIE",
    TV = "TV"
}

export class MovieGroupBy {
    mediaType!: MediaType; // <----- What I'm expecting
}

new MovieGroupBy().mediaType = MediaType.TV; // OK
new MovieGroupBy().mediaType = 'TV'; // Valid, but TS Error


export class MovieGroupByB {
    mediaType!: keyof typeof MediaType;
}

new MovieGroupByB().mediaType = MediaType.TV; // OK
new MovieGroupByB().mediaType = 'TV';  // OK

Enum in schema is generated because of @Field(() => MediaType, {nullable:false}) this decorator mediaType!: MediaType this statement make no sense for nestjs/graphql.

Could you provide minimal repo to reproduce the error?

unlight avatar Jul 13 '22 07:07 unlight