[Related to https://github.com/graphql/graphiql/issues/58]
I have two node modules A => B, where B creates a GraphQLSchema object that is imported by A, which is then used to instantiate graphqlExpress.
I get this error:
Ensure that there are not multiple versions of GraphQL installed in your node_modules directory
And see that I have 2 different instances of the GraphQLSchema class type at runtime -- so validate.js invariant(schema instanceof GraphQLSchema) fails because the imported schema is an instance of A's GraphQLSchema not B's GraphQLSchema.
However, all npm dependencies are of the same version (in both A and B modules).
> npm --version
3.10.7
> babel-node --version
6.16.0
> find node_modules -name graphql
node_modules/graphql
node_modules/graphql-subscriptions/node_modules/graphql
> grep version node_modules/graphql/package.json
0.7.2
> grep version node_modules/graphql-subscriptions/node_modules/graphql/package.json
0.7.2
I assume this is a common pattern, but I can't find an example.
BTW, I'm using npm-link to create the A => B dependency.
Using peerDependencies in A/package.json I managed to get rid of node_modules/graphql-subscriptions/node_modules/graphql:
"peerDependencies": {
"graphql": "^0.7.0",
"graphql-subscriptions": "^0.2.1"
}
But the problem remains since A's GraphQLSchema class is different from B's.
This seems like a problem with npm or possibly the graphql-subscriptions package, not with graphql-js.
Probably right, but this seemed like a common pattern?
Sure, I guess I'm just saying there isn't much graphql-js can do about this. It's a good thing that it checks that you only have one instance, otherwise there would be much worse problems.
I think this is the solution (if you're using webpack):
resolve: {
alias: {
graphql: path.resolve('./node_modules/graphql'),
react: path.resolve('./node_modules/react') // Same issue.
}
}
Got this issue as well.
Using node 7.9
$ find node_modules -name graphql
node_modules/@types/graphql
node_modules/graphql
should i just delete the @types/graphql directory?
I don't think this should affect it but...
@casoetan: deleting it sounds like a reasonable bet.
I get the same results of casoetan.
deleting @types/graphql doesnt seem to have an effect
The issue seems to be the way i called makeExecutableSchema. Solved this by using 'graphql-modules'.
@stubailo I resolved this for webpack some time ago, but the problem still exists for monorepos that have different sub packages referencing modules that depend on graphql.
I tried lerna --hoist to resolve, but this is flaky.
(BTW, I removed the dependency on graphql-subscriptions (red herring).
I've also tried moving graphql deps into peerDependencies for my own sub packages that my failing package depends on.
In the module where the error occurs (as above):
find -L node_modules -name graphql
node_modules/@types/graphql
node_modules/test-api/node_modules/@types/graphql
node_modules/test-api/node_modules/test-core/node_modules/@types/graphql
node_modules/test-api/node_modules/test-core/node_modules/graphql
node_modules/test-client/node_modules/@types/graphql
node_modules/test-client/node_modules/test-api/node_modules/@types/graphql
node_modules/test-client/node_modules/test-api/node_modules/test-core/node_modules/@types/graphql
node_modules/test-client/node_modules/test-api/node_modules/test-core/node_modules/graphql
node_modules/test-client/node_modules/test-core/node_modules/@types/graphql
node_modules/test-client/node_modules/test-core/node_modules/graphql
node_modules/test-client/node_modules/graphql
node_modules/test-core/node_modules/@types/graphql
node_modules/test-core/node_modules/graphql
node_modules/graphql
where test-* are linked modules (all of which define graphql as peerDependencies.
I assume @types/graphql are normal TypeScript deps?
Closing this aging issue.
graphql-js is not designed to support using functionality between different installations of itself. The instanceof is only one example where this failure occurs - but there are others as well, especially if there would ever be inadvertent differences in released versions.
If you use packages with sub-packages, ensure they support ranges of versions for graphql so that npm or yarn can find a single version to install at the top level which works for all dependents.
A note for those still encountering this issue, yarn is pretty good at doing this version resolution, including support for a flag which enforces guarantees for a single version of every dependency if you want that. I believe npm5 also has some support for this kind of guarantee
I'm using yarn and have the same error
I have also faced this error, for example, my express-graphql uses [email protected] but I had installed [email protected] due to graphql-sequelize-crud uses a old version of graphql. I checked this out by this command
$ find node_modules -name graphql
node_modules/express-graphql/node_modules/graphql
node_modules/graphql
So I just run rm -rf node_modules/express-graphql/node_modules/graphql here and everything solved.
Note that basically you need to only have 1 line result for find node_modules -name graphql
I'm facing this problem today using yarn. I believe the source of this is that graphql-js is still using alpha versions (0.x...) and therefore the normal semver ranges don't apply. So #1005 needs to be fixed.
I have only a single instance of graphql in node_modules.
I have a shared GraphQLObject instance that is created in a library and I have resolved that
main: console.log(case 1): graphql path is ${require.resolve('graphql')}) and
library: console.log(case 2): graphql path is ${require.resolve('graphql')}) both resolve to the same path.
Yet I get the instanceof error flagged - that somehow it detects that the GraphQLObject object are belonging to different graphl instances.
@nishant-dani Do you have a bundler or minifier corrupting the exports?
Most helpful comment
I have also faced this error, for example, my
express-graphqluses[email protected]but I had installed[email protected]due tographql-sequelize-cruduses a old version of graphql. I checked this out by this commandSo I just run
rm -rf node_modules/express-graphql/node_modules/graphqlhere and everything solved.Note that basically you need to only have 1 line result for
find node_modules -name graphql