Relay: Invariant error when printing Enums

Created on 4 Feb 2019  路  4Comments  路  Source: facebook/relay

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)

bug good first bug

Most helpful comment

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!

All 4 comments

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

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!

Cool, I can make a fix..

@freiksenet let me know if you need some help

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fedbalves picture fedbalves  路  3Comments

johntran picture johntran  路  3Comments

piotrblasiak picture piotrblasiak  路  3Comments

bondanherumurti picture bondanherumurti  路  3Comments

HsuTing picture HsuTing  路  3Comments