Graphql-tools: GraphQLEnumType does not work if there's a enum in the schema

Created on 30 Sep 2017  Â·  6Comments  Â·  Source: ardatan/graphql-tools

I have this schema

enum MyEnum {
  itemA
  itemB
}

type Query {
  enum: MyEnum
}

If I provide a resolver like this

{
  MyEnum: new GraphQLEnumType({
      name: 'MyEnum',

      values: {
        itemA: { value: 0 },
        itemB: { value: 1 }
    }
}

I get an error like Error: MyEnum was defined in resolvers, but it's not an object

Most helpful comment

@stubailo, my apologies, my libraries were pretty out of date. In fact, this error goes away with a new version of graphql-tools (2.16.0).

It looks like what I'm struggling with now relates to the issue referenced in PR 364 and Issue 525, among others. Specifically, my difficulty is defining a nullable enum. As it is, even if I add a key of NULL to the enum and assign it the value of null in the resolver object, I still get the error "Expected a value of type "EnumType" but received: " when null is returned from the query. And, according to the GraphQL github, null values should be allowed on enums - it just seems like there needs to be a way to define them in the schema using graphql-tools.

The only workaround I've found is to define a custom scalar that returns a string instead of null for the null value case, and add that to the resolver. In this case, the scalar description is what I'm left with to document this part of the schema, which isn't as nice as being able to click through to see the accepted values (including null) listed out in GraphiQL.

Any further insights are appreciated.

All 6 comments

BTW, if I remove the resolver, then it works, but I need to map the values as numbers

Same issue

I get the same error if I follow the docs and assign an object as the resolver for the enum. Like @diestrin, I also tried using GraphQLEnumType to define the values, but this doesn't work for my use case, which was an attempt to define a default null value (to avoid the error "Expected a value of type 'EnumType' but received" ...).

Odd - there are definitely tests for this, and the correct way is to not use GraphQLEnumType but just pass the object with values: https://github.com/apollographql/graphql-tools/pull/508/files#diff-d1c31d9764c4c5294b2d2d4dd94457a7R60

Are you using the newest version of graphql-tools?

@stubailo, my apologies, my libraries were pretty out of date. In fact, this error goes away with a new version of graphql-tools (2.16.0).

It looks like what I'm struggling with now relates to the issue referenced in PR 364 and Issue 525, among others. Specifically, my difficulty is defining a nullable enum. As it is, even if I add a key of NULL to the enum and assign it the value of null in the resolver object, I still get the error "Expected a value of type "EnumType" but received: " when null is returned from the query. And, according to the GraphQL github, null values should be allowed on enums - it just seems like there needs to be a way to define them in the schema using graphql-tools.

The only workaround I've found is to define a custom scalar that returns a string instead of null for the null value case, and add that to the resolver. In this case, the scalar description is what I'm left with to document this part of the schema, which isn't as nice as being able to click through to see the accepted values (including null) listed out in GraphiQL.

Any further insights are appreciated.

Hey there hasn't been discussion on this in a while, and it looks like the original error isn't the issue at hand anymore. If you still have a problem related to nullable enums, please open a new issue on this repo!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Adherentman picture Adherentman  Â·  4Comments

stubailo picture stubailo  Â·  3Comments

confuser picture confuser  Â·  4Comments

benjaminhon picture benjaminhon  Â·  3Comments

avnersorek picture avnersorek  Â·  3Comments