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:

  1. Changing the version of graphql in your project's package.json file to match exactly what graphql-tools depends on in its package.json file.

  2. Removing graphql as a dependency and just installing graphql-tools. Then you will automatically receive whatever graphql module version that graphql-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)