Closed ComBarnea closed 4 years ago
Please provide a minimum reproduction repository.
@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 :)
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
.
@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.
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.
What is the reason behind it working like that?
@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
Could anyone find a way to use enum values instead of keys?
Have the same issue, why is this closed ?
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.
Has anyone been able to make this work with a code first approach?
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!
I'm submitting a...
Current behavior
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