importexpressfrom'express';import{Schema}from'./data/schema';importgraphQLHTTPfrom'express-graphql';// ADD: import fot the print utilitiesimport{printSchema}from'graphql/utilities/schemaPrinter';importpathfrom'path';constapp=express();app.use(express.static(path.resolve(__dirname,'public')));// ADD: a middleware to call the print on the Schemaapp.use('/schema',function(req,res,next){res.set('Content-Type','text/plain');res.send(printSchema(Schema));});app.use('/',graphQLHTTP({schema:Schema,pretty:true}));app.listen(8080,(err)=>{if(err){returnconsole.error(err);}console.log('GraphQL Server is now running on localhost:8080');});

And, as I may have hinted you with the title of the section, it didn’t work:

Error
at invariant (/Users/pierrecarion/work/graphql-relay-authentication/build/webpack:/~/graphql/jsutils/invariant.js:20:1)
at printType (/Users/pierrecarion/work/graphql-relay-authentication/build/webpack:/~/graphql/utilities/schemaPrinter.js:81:1)
at Array.map (native)
at printFilteredSchema (/Users/pierrecarion/work/graphql-relay-authentication/build/webpack:/~/graphql/utilities/schemaPrinter.js:64:1)

I did that about 10 times in the past and it always worked!

After some debugging, I found the code throwing this error in graphql/utilities/schemaPrinter.js:

functionprintType(type){if(typeinstanceof_typeDefinition.GraphQLScalarType){returnprintScalar(type);}elseif(typeinstanceof_typeDefinition.GraphQLObjectType){returnprintObject(type);}elseif(typeinstanceof_typeDefinition.GraphQLInterfaceType){returnprintInterface(type);}elseif(typeinstanceof_typeDefinition.GraphQLUnionType){returnprintUnion(type);}elseif(typeinstanceof_typeDefinition.GraphQLEnumType){returnprintEnum(type);}// Exception thrown by this invariant:(0,_jsutilsInvariant2['default'])(typeinstanceof_typeDefinition.GraphQLInputObjectType);returnprintInputObject(type);}

If I removed the invariant, the schema was being displayed correctly…

I added a log to display the type of the type:

console.log('@@@ ctor name:'+type.constructor.name);

and the log printed: GraphQLObjectType but for some reason this type was not caught in the previous if statements.

This kind of bug reminded me of the good old times of java programming where such a test could fail could happen if the same class was
loaded by 2 different class loaders.

No class loader here in javascript but … the skeleton code was using webpack to bundle the server code.

No webpack for server

webpack is usually used to bundle javascript code for the web components.

Using webpack, you can write your front end code using the regular import/require code: webpack processes all your client code and generate a single javascript code with the dependencies resolved (webpack can do much more than that but we’ll cover that in another post)

Even though there is no reason not to use webpack for server code, I do think that the error was caused by webpack.

My thinking is that there was 2 GraphQLObjectType classes in the runtime:

the class bundled inside the server.js file through webpack

an instance loaded by the usual node require mechanism

No proof for that intuition, but as soon as I removed webpack to bundle the server code, the error disappeared.

babel to bundle server code

A more usual way to bundle ES6 server code is to use babel (along with grunt here):