nestjs / graphql

GraphQL (TypeScript) module for Nest framework (node.js) 🍷
https://docs.nestjs.com/graphql/quick-start
MIT License
1.46k stars 397 forks source link

Enum key instead of value #818

Closed ComBarnea closed 4 years ago

ComBarnea commented 4 years ago

I'm submitting a...


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Screen Shot 2020-04-27 at 1 32 03 PM Screen Shot 2020-04-27 at 1 31 56 PM Screen Shot 2020-04-27 at 1 31 45 PM Screen Shot 2020-04-27 at 1 37 42 PM

Issue is just like in ticket https://github.com/nestjs/graphql/issues/227. I am getting Enum key and not enum value. Just to make sure, I checked all the code and there is no use of type-graphql, just nestjs/graphql.

Expected behavior

It should have return inner value.

Minimal reproduction of the problem with instructions

If needed I will create small project.

Environment


@nestjs/common@7.0.8
@nestjs/core@7.0.8
@nestjs/graphql@7.3.4
@nestjs/jwt@^7.0.0
@nestjs/mongoose@^6.4.0
@nestjs/passport@^7.0.0
@nestjs/platform-express@7.0.8
@nestjs/platform-socket.io@7.0.8
@nestjs/swagger@4.5.3
@nestjs/terminus@7.0.1
@nestjs/websockets@7.0.8



For Tooling issues:
- Node version: 11.15.0
- Platform:  Mac

Others:

kamilmysliwiec commented 4 years ago

Please provide a minimum reproduction repository.

ComBarnea commented 4 years ago

@kamilmysliwiec sure thing buddy, I created small Repo and checked again, let me know if I can help. https://github.com/ComBarnea/nest-grpah-enum-check

BTW, Thanks for the amazing work, I am using Nest since version 2 :)

kamilmysliwiec commented 4 years ago

I'm glad you like it!

I've just tested your example and this is exactly how it is supposed to work. You return the waiting string in your code - GrapQL automatically maps it to the enum definition (WAITING). Likewise, if you passed WAITING, GQL would automatically map to waiting.

lazizcodes commented 2 years ago

@kamilmysliwiec Can we force it somehow to generate only from enum values (not keys)?

enum BlaType {
    BlaBla = 'bla bla'
}

i.e. there is a space between that. I can't just remove that space since I already have a bunch of data like so.

youbek commented 2 years ago

When doing testing what is the best approach here?

For example:

enum UserType {
   DistrictAdmin = "District admin",
   SchoolAdmin = "School admin"
}

expect(queryResult).toBe(UserType.DistrictAdmin);

This fails because GraphQL resolves to string "DistrictAdmin" not "District admin". How do you guys test this?

It really feels like GraphQL should return value to the client instead of the key which seems more like an implementation detail of the value.

damaon commented 1 year ago

What is the reason behind it working like that?

adam-tkaczyk-mck commented 1 year ago

@lazizcodes You can create enums with spaces. Take a look here.

But there are use-cases when enum is like

enum Role {
  Admin = 'ADMIN',
  User = 'USER'
}

And we want generate GQL enum from values like

enum Role {
  ADMIN
  USER
}

Pothos has a very good API to achieve that: https://pothos-graphql.dev/docs/guide/enums

rohanrajpal commented 1 year ago

Could anyone find a way to use enum values instead of keys?

alex20465 commented 10 months ago

Have the same issue, why is this closed ?

alex20465 commented 10 months ago

For everyone else having the same issue:

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

You have to implement a root enum resolver which resolves the value, see RED example in the documentation.

visormatt commented 9 months ago

Has anyone been able to make this work with a code first approach?

maxkomarychev commented 6 months ago

Hi, the solution with custom resolvers does not work with code-first approach. Can you please provide a recipe how to fix the problem? Thank you!