Error: Cannot use GraphQLSchema "[object GraphQLSchema]" from another module or realm
This situation may also occur when the version of the graphql
module you have installed is different from the version installed and used by graphql-tools
.
I have found you can correct this by either:
Changing the version of
graphql
in your project'spackage.json
file to match exactly whatgraphql-tools
depends on in itspackage.json
file.Removing
graphql
as a dependency and just installinggraphql-tools
. Then you will automatically receive whatevergraphql
module version thatgraphql-tools
installs (as long as you don't depend on any other packages that install another, conflicting version).
In other cases you might have the correct version, but it may be installed multiple times. You can use npm ls graphql
to see all the installed versions. Try running npm dedupe
to remove duplicate installations.
This happens because graphql-tools
module imports graphql
from its CommonJS module, while my code does it from the ES module. That is, each object in my own module comes from the ES module, while graph-tool
's not.
Solution
It's as easy as importing anything from graphql
importing the CommonJS module, and both objects from graphql
and graphql-tools
will be able to talk each together:
import graphql_ from 'graphql/index.js'
import graphqlTools from 'graphql-tools'
const { graphql } = graphql_
const { makeExecutableSchema } = graphqlTools
const typeDefs = `
type Query {
as: [A]
}
type A {
x: Int,
y: Int
}
`
const schema = makeExecutableSchema ({ typeDefs })
graphql(schema, '{ as { x, y } }').then(console.log)