Reputation: 133
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?
Upvotes: 4
Views: 4748
Reputation: 1
I just put it key-value and it worked with prisma.
enum Role {
USER = 'USER',
ADMIN = 'ADMIN',
}
registerEnumType(Role, {
name: 'Role',
});
export class CreateUserInput {
@IsEnum(Role)
@Field(() => Role, { nullable: true })
role?: Role;
}
and do it like this in the service:
async create(createUserInput: CreateUserInput): Promise<User> {
return this.prisma.user.create({
data: {
...createUserInput,
},
});
}
and It worked.Hope it help
Upvotes: 0
Reputation: 543
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
Upvotes: 8
Reputation: 5736
You're probably missing the registration of the enum type in GraphQL:
// user.model.ts
registerEnumType(Level, { name: "Level" });
Upvotes: 1