question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to get the internal value of enum instead of key?

See original GitHub issue

I’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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
kamilmysliwieccommented, May 14, 2019

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)

1reaction
marvinrogercommented, May 11, 2019

I can confirm it’s fixed in next, thanks @kamilmysliwiec!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found