Error: Cannot use GraphQLSchema "[object GraphQLSchema]" from another module or realm
Asked Answered
C

6

19

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?

Catabolism answered 3/10, 2018 at 21:26 Comment(0)
C
8

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)
Catabolism answered 3/10, 2018 at 21:26 Comment(3)
I have filed an issue with graphql-tools trying to sort that but while your patch works we cannot find source of issue.. Any ideas? still does it in latest versionsMise
@Mise Would you mind sharing the issue?Chlorobenzene
@PatrickDesjardins turns out that graphql-tools fixed that issue a while back so was not actually the culprit for me. My issue was that yarn was not upgrading graphql-tools fully - it oddly would tell me it upgraded but kept old files. A full node_modules delete/install with latest graphql-tools fixed the issue.Mise
R
16

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.

Rimose answered 25/3, 2020 at 2:8 Comment(1)
This worked for me, the ls command gave me context, but the dedupe cleaned it up.Apiary
C
8

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)
Catabolism answered 3/10, 2018 at 21:26 Comment(3)
I have filed an issue with graphql-tools trying to sort that but while your patch works we cannot find source of issue.. Any ideas? still does it in latest versionsMise
@Mise Would you mind sharing the issue?Chlorobenzene
@PatrickDesjardins turns out that graphql-tools fixed that issue a while back so was not actually the culprit for me. My issue was that yarn was not upgrading graphql-tools fully - it oddly would tell me it upgraded but kept old files. A full node_modules delete/install with latest graphql-tools fixed the issue.Mise
S
6

My problem was both .js an .mjs graphql files are resolved due to wrong webpack configuration.

Root cause:

From TypeMapper.mjs file in graphql-compose, import statement does not have file extension and that was a failure on webpack bundle. In order to solve it, I required to add fullySpecified:false into the webpack config.

{
  test: /\.m?js/,
  include: /node_modules/,
  type: "javascript/auto",
  resolve: {
    fullySpecified: false
  }
}

And I also modified resolve statement like

resolve: {
  extensions: [".ts", ".js", ".mjs"] // that was the actual problem
}

Since fullySpecified config has been set to false, webpack was trying to resolve files without extension respect to the order of resolve.extentions config. Due to the wrong order in that config, graphql files with .js ext were been resolving although all other files were using .mjs one.

Solution:

Simply re-order resolve.extensions config as

resolve: {
  extensions: [".ts", ".mjs", ".js"]
}
Soledadsolely answered 12/3, 2021 at 11:1 Comment(0)
D
3

In my case, I added the webpack-node-externals library to the webpack configuration, and I was able to run my bundled application.

    externalsPresets: { node: true },
    externals: [nodeExternals()],

I am using webpack version 5.*

Also I am using yarn package manager, so I added resolutions in my package.json

  "resolutions": {
    "graphql": "^15.3.0"
  }
Damnify answered 23/3, 2022 at 7:8 Comment(0)
W
2

For me, it was solved by downgrading some packages like this:

"apollo": "^2.33.4", "graphql": "^15.5.0",

I also deleted node_modules and package-lock.json and installed packages with yarn instead of npm.

Wakerife answered 16/7, 2022 at 19:1 Comment(0)
M
0

I got this exact error when my .npmrc did not have proper entries such as username and password. We are using jFrog to normalise package installation. .npmrc should be located at root with proper entries. ex: .npmrc file which works

@<company-name>:registry=<registry-url>
//<artifactory-name>:_password=${PASSWORD}
//<artifactory-name>:username=${JFROG_USERNAME}
//<artifactory-name>:email=${YOUR_EMAIL}
//<artifactory-name>:always-auth=true
Mneme answered 13/4, 2021 at 8:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.