Skip to content
Advertisement

Error: Cannot use GraphQLSchema “[object GraphQLSchema]” from another module or realm

Given the following code:

import { graphql } from 'graphql'
import graphqlTools from 'graphql-tools'

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)

I get this error:

Error: Cannot use GraphQLSchema “[object GraphQLSchema]” from another module or realm.

Ensure that there is only one instance of “graphql” in the node_modules directory. If different versions of “graphql” are the dependencies of other relied on modules, use “resolutions” to ensure only one version is installed.

What’s going on?

Advertisement

Answer

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)
Advertisement