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

mixing ES6 import and require triggers misleading error #1309

Closed
stevvvn opened this issue Apr 7, 2018 · 2 comments
Closed

mixing ES6 import and require triggers misleading error #1309

stevvvn opened this issue Apr 7, 2018 · 2 comments

Comments

@stevvvn
Copy link

stevvvn commented Apr 7, 2018

The following does not work, though it seems to me it should (node@9.8.0):

import graphqlHTTP from 'express-graphql'; // @0.6.12
import { buildSchema } from 'graphql'; // @1.13.2
import express from 'express';

const app = express();
app.use('/graphql', graphqlHTTP({
  'schema': buildSchema('type Query { hello: String }'),
  'rootValue': { 'hello': () => 'Hello, world!' }
}));
app.listen(4000);
$ curl -s -X POST -H "Content-type: application/json" http://localhost:4000/graphql --data '{ "query": "{ hello }" }'|jq
{
  "errors": [
    {
      "message": "Cannot use GraphQLSchema \"[object Object]\" from another module or realm.\n\nEnsure that there is only one instance of \"graphql\" in the node_modules\ndirectory. If different versions of \"graphql\" are the dependencies of other\nrelied on modules, use \"resolutions\" to ensure only one version is installed.\n\nhttps://yarnpkg.com/en/docs/selective-version-resolutions\n\nDuplicate \"graphql\" modules cannot be used at the same time since different\nversions may have different capabilities and behavior. The data from one\nversion used in the function from another could produce confusing and\nspurious results."
    }
  ]
}

Creating a wrapper to require buildSchema instead:

req.js

module.exports = require('graphql').buildSchema;

And changing the initial example:

//import { buildSchema } from 'graphql';
import buildSchema from './req';

works as expected:

$ curl -s -X POST -H "Content-type: application/json" http://localhost:4000/graphql --data '{ "query": "{ hello }" }'|jq
{
  "data": {
    "hello": "Hello, world!"
  }
}

Arguably this is a problem with express-graphql; it could be resolved by their build including a .mjs file that imports GraphQL to match like-for-like module conventions, instead of just a .js file that requires it regardless of context. However, I think GraphQL is wrong to complain about the mixed arrangement, and in any case the error message is a red herring.

@IvanGoncharov
Copy link
Member

@stevvvn Thanks for reporting 👍

However, I think GraphQL is wrong to complain about the mixed arrangement, and in any case the error message is a red herring.

It's intentional, see here: #491 (comment)

Arguably this is a problem with express-graphql;

Would be great if you report this issue on express-graphql and I will try to fix it in the next release.

@stevvvn
Copy link
Author

stevvvn commented Apr 7, 2018

Thanks for the reference!

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

No branches or pull requests

2 participants