Skip to content

How to use enum in resolver? #445

Discussion options

You must be logged in to vote

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:

export enum Gender {
  MALE,
  FEMALE,
}

builder.enumType(Gender, { 
  name: 'Gender',
})

builder.prismaObject('Person', {
  findUnique: (person) => ({ id: person.id }),
  fields: (t) => ({
    id: t.exposeString('id'),
    name: t.exposeString('name'),
    gender: t.field({
      type: Gender,
      resolve: (person) => Gender[person.gender as keyof typeof Gender],
    }),
  }),
})

Thx for your help.

Replies: 3 comments 13 replies

Comment options

You must be logged in to vote
5 replies
@marceloverdijk
Comment options

@hayes
Comment options

@marceloverdijk
Comment options

@hayes
Comment options

@abdofola
Comment options

Comment options

You must be logged in to vote
7 replies
@AnthonyZhang220
Comment options

@hayes
Comment options

@AnthonyZhang220
Comment options

@max-petrenko
Comment options

@arimgibson
Comment options

Answer selected by marceloverdijk
Comment options

You must be logged in to vote
1 reply
@max-taylor
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
7 participants