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?
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
You're probably missing the registration of the enum type in GraphQL:
// user.model.ts
registerEnumType(Level, { name: "Level" });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With