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

Created on 29 Apr 2019  路  6Comments  路  Source: nestjs/graphql

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
done

All 6 comments

I've been facing that exact same issue, too. Directly using type-graphql works fine.

It should be fixed in the @next version ($ npm i @nestjs/graphql@next). You will also have to update @nestjs/core to 6.2.0 ($ npm i @nestjs/[email protected])

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

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)

Could GraphQLDefinitionsFactory generate definitions with custom enum values?

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rshea0 picture rshea0  路  3Comments

fenos picture fenos  路  5Comments

Warchant picture Warchant  路  4Comments

joseym picture joseym  路  4Comments

liudonghua123 picture liudonghua123  路  3Comments