How to use enum in resolver? #445
-
I'm trying to use the following
In the database it is not a Then with this
it complains about the
It seems it cannot map the Do I need to convert it somehow? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 13 replies
-
Enums can be used in 2 ways. You can either use a typescript enum (eg if this is an enum that exists in your prisma schema, you can import that. In that case, you pass the enum when defining it: import { MyEnum } from '@prisma/client'
builder.enumbType(MyEnum, { name: 'MyEnum' })
builder.prismaObject('Person', {
fields: (t) => ({
myField: t.field({
type: MyEnum,
resolve: (parent) => MyEnum.enumValue,
}),
myField: t.expose('myField', {
type: MyEnum,
}),
}) Or you can create your own enums, in this case you need the return value of the builder.enumType method: const MyEnum = builder.enumbType('MyEnum', { values: ['SomeValue'] as const })
builder.prismaObject('Person', {
fields: (t) => ({
myField: t.field({
type: MyEnum,
resolve: (parent) => 'SomeValue'
}),
myField: t.expose('myField', {
type: MyEnum,
}),
}) |
Beta Was this translation helpful? Give feedback.
-
As I don't want to repeat the enum values I came up with this solution which to me is the most clean + shortest on code:
Thx for your help. |
Beta Was this translation helpful? Give feedback.
-
You can also define a
then use it this way:
|
Beta Was this translation helpful? Give feedback.
As I don't want to repeat the enum values I came up with this solution which to me is the most clean + shortest on code:
Thx for your help.