How to get the internal value of enum instead of key?
See original GitHub issueI’m trying to reproduce this example, after 3 hours testing, I have no idea how to get the values of enum using nestjs/graphql
(type-graphql
works fine).
## I'm submitting a...
[ ] Regression
[ ] 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
Let’s say we have an enum:
import { registerEnumType } from "type-graphql";
export enum Difficulty {
Beginner = 1,
Easy = 2,
Medium = 3,
Hard = 4,
MasterChef = 5,
}
registerEnumType(Difficulty, {
name: "Difficulty",
});
And the resolver
:
import {
Resolver,
Args,
} from '@nestjs/graphql';
import {
Recipe,
} from './recipe.entity';
import { Difficulty } from './recipe.constant';
@Resolver(Recipe)
export class RecipeResolver {
@Query(returns => [Recipe])
recipes(@Args({ name: 'difficulty', type: () => Difficulty }) difficulty: Difficulty) {
console.log(difficulty); // I'm getting the string key name
}
}
Query:
query {
recipes (difficulty: Beginner) {
name
}
}
The console output is "Beginner"
.
Expected behavior
Should be 1
.
Minimal reproduction of the problem with instructions
If it’s necessary, I’ll creating one.
What is the motivation / use case for changing the behavior?
Common usage.
Environment
Nest version: 6.1.1
For Tooling issues:
- Node version: 10.15.3
- Platform: Windows
Others:
- TypeScript: 3.4.3
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Get enum by its inner field - Stack Overflow
Here is the most convenient way to find enum value by its field: ... You should have a HashMap with the numbers as...
Read more >How To Play With Enum in C# - C# Corner
How to get enum variable name by its value in C#?. Retrieves the name of the constant in the specified enumeration that has...
Read more >Get an Enum Key by Value in TypeScript | bobbyhadz
To get an enum key by value, use the `Object.values()` method to get an array of the enum's values. Use the `indexOf()` method...
Read more >Java Enum Tutorial: 10 Examples of Enum in Java
1) Enums in Java are type-safe and have their own namespace. It means your enum will have a type for example "Currency" in...
Read more >Enums - TypeGraphQL
It can be a numeric or string enum - the internal values of enums are taken from the enum definition values and the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Published as
6.2.0
($ npm i @nestjs/graphql@latest
). You will also have to update@nestjs/core
to 6.2.0 ($ npm i @nestjs/core@latest
)I can confirm it’s fixed in
next
, thanks @kamilmysliwiec!