'Using enums from prisma in nestJS graphQL models

My object that is supposed to be returned:

@ObjectType()
export class User {
  @Field(() => String)
  email: string

  @Field(() => [Level])
  level: Level[]
}

Level is an enum generated by prisma, defined in schema.prisma:

enum Level {
  EASY
  MEDIUM
  HARD
}

Now I'm trying to return this User object in my GraphQL Mutation:

@Mutation(() => User, { name: 'some-endpoint' })

When running this code, I'm getting the following error:

UnhandledPromiseRejectionWarning: Error: Cannot determine a GraphQL output type for the "Level". Make sure your class is decorated with an appropriate decorator.

What am I doing wrong here? Can't enums from prisma be used as a field?



Solution 1:[1]

You're probably missing the registration of the enum type in GraphQL:

// user.model.ts
registerEnumType(Level, { name: "Level" });

Solution 2:[2]

Of course, you can.

import { Level } from '@prisma/client'

@ObjectType()
export class User {
  @Field(() => String)
  email: string

  @Field(() => Level)
  level: Level
}

registerEnumType(Level, {
  name: 'Level',
});

You should use registerEnumType + @Field(() => Enum)

https://docs.nestjs.com/graphql/unions-and-enums#enums

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Joulukuusi
Solution 2 Émerson Felinto