Skip to content

mixing ES6 import and require triggers misleading error #1309

Closed
@stevvvn

Description

@stevvvn

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions