Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invariant error when printing Enums #2633

Closed
freiksenet opened this issue Feb 4, 2019 · 4 comments
Closed

Invariant error when printing Enums #2633

freiksenet opened this issue Feb 4, 2019 · 4 comments

Comments

@freiksenet
Copy link
Contributor

freiksenet commented Feb 4, 2019

Given the schema:

var graphql = require('graphql')

var TestEnumType = new graphql.GraphQLEnumType({
  name: 'TestEnum',
  values: {
    FOO: {
      value: 1
    },
    BAR: {
      value: 2
    }
  }
})

var schema = new graphql.GraphQLSchema({
  query: new graphql.GraphQLObjectType({
    name: 'Query',
    fields: {
      print: {
        type: graphql.GraphQLString,
        args: {
          test: {
            type:  TestEnumType
          }
        }
     }
   }
 })
})

Having literal enum in the query results in error

relayCompiler.Printer.print(
  relayCompiler.Parser.parse(
    schema, 
    'query Foo { print(test: FOO) }'
  )[0]
)
Invariant Violation: GraphQLIRPrinter: Expected value of type TestEnum to be a string, got `1`.
    at invariant (/home/freiksenet/Work/gatsby/gatsby/node_modules/fbjs/lib/invariant.js:40:15)
    at printLiteral (/home/freiksenet/Work/gatsby/gatsby/node_modules/relay-compiler/lib/GraphQLIRPrinter.js:255:105)
    at printValue (/home/freiksenet/Work/gatsby/gatsby/node_modules/relay-compiler/lib/GraphQLIRPrinter.js:243:12)
    at /home/freiksenet/Work/gatsby/gatsby/node_modules/relay-compiler/lib/GraphQLIRPrinter.js:212:24
    at Array.forEach (<anonymous>)
    at printArguments (/home/freiksenet/Work/gatsby/gatsby/node_modules/relay-compiler/lib/GraphQLIRPrinter.js:211:8)
    at printField (/home/freiksenet/Work/gatsby/gatsby/node_modules/relay-compiler/lib/GraphQLIRPrinter.js:56:81)
    at printSelection (/home/freiksenet/Work/gatsby/gatsby/node_modules/relay-compiler/lib/GraphQLIRPrinter.js:74:11)

@freiksenet
Copy link
Contributor Author

Seems that relay is replacing all enums with literal values, which breaks for enums that don't have string values.

@josephsavona
Copy link
Contributor

Thanks for reporting this, looks like this is a supported use of GraphQLEnumType in graphql-js, so we should support it. I think the issue is that printLiteral should be calling type.serialize(literalValue) instead of trying to emit a string value. PRs welcome!

@freiksenet
Copy link
Contributor Author

Cool, I can make a fix..

@saranshkataria
Copy link

@freiksenet let me know if you need some help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants