Error: Cannot use GraphQLSchema "[object GraphQLSchema]" from another module or realm
Error: Cannot use GraphQLSchema "[object GraphQLSchema]" from another module or realm
给定以下代码:
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)
我收到这个错误:
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.
怎么回事?
发生这种情况是因为 graphql-tools
模块从其 CommonJS 模块导入 graphql
,而我的代码是从 ES 模块导入的。也就是说,我自己的模块中的每个对象都来自 ES 模块,而 graph-tool
则不是。
解决方案
就像从 graphql
导入 CommonJS 模块一样简单,来自 graphql
和 graphql-tools
的两个对象将能够相互交流:
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)
当您安装的graphql
模块版本与graphql-tools
安装使用的版本不同时,也会出现这种情况。
我发现您可以通过以下任一方法更正此问题:
更改项目 package.json
文件中 graphql
的版本,使其与 graphql-tools
在其 package.json
文件中所依赖的完全匹配。
删除 graphql
作为依赖项并仅安装 graphql-tools
。然后您将自动收到 graphql-tools
安装的任何 graphql
模块版本(只要您不依赖于安装另一个冲突版本的任何其他包)。
在其他情况下,您可能拥有正确的版本,但可能会安装多次。您可以使用 npm ls graphql
查看所有已安装的版本。尝试 运行 npm dedupe
删除重复安装。
我的问题是 .js
和 .mjs
graphql 文件由于错误的 webpack 配置而被解决。
根本原因:
来自 TypeMapper.mjs
文件 graphql-compose
,import 语句没有文件扩展名,这是 webpack 包失败的原因。为了解决这个问题,我需要将 fullySpecified:false
添加到 webpack 配置中。
{
test: /\.m?js/,
include: /node_modules/,
type: "javascript/auto",
resolve: {
fullySpecified: false
}
}
而且我还修改了 resolve 语句,比如
resolve: {
extensions: [".ts", ".js", ".mjs"] // that was the actual problem
}
由于 fullySpecified
配置已设置为 false
,webpack 尝试根据 resolve.extentions
配置的顺序解析没有扩展名的文件。由于该配置中的错误顺序,graphql
分机号为 .js
的文件正在解析,尽管所有其他文件都使用 .mjs
分机号。
解法:
只需将 resolve.extensions
配置重新排序为
resolve: {
extensions: [".ts", ".mjs", ".js"]
}
当我的 .npmrc 没有正确的条目(例如用户名和密码)时,我得到了这个确切的错误。我们正在使用 jFrog 规范包安装。 .npmrc 应该位于具有适当条目的根目录下。
例如:有效的 .npmrc 文件
@<company-name>:registry=<registry-url>
//<artifactory-name>:_password=${PASSWORD}
//<artifactory-name>:username=${JFROG_USERNAME}
//<artifactory-name>:email=${YOUR_EMAIL}
//<artifactory-name>:always-auth=true
在我的例子中,我将 webpack-node-externals
库添加到 webpack 配置中,并且我能够 运行 我的捆绑应用程序。
externalsPresets: { node: true },
externals: [nodeExternals()],
我正在使用 webpack 版本 5.*
我也在使用 yarn
包管理器,所以我在 package.json
中添加了 resolutions
"resolutions": {
"graphql": "^15.3.0"
}
给定以下代码:
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)
我收到这个错误:
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.
怎么回事?
发生这种情况是因为 graphql-tools
模块从其 CommonJS 模块导入 graphql
,而我的代码是从 ES 模块导入的。也就是说,我自己的模块中的每个对象都来自 ES 模块,而 graph-tool
则不是。
解决方案
就像从 graphql
导入 CommonJS 模块一样简单,来自 graphql
和 graphql-tools
的两个对象将能够相互交流:
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)
当您安装的graphql
模块版本与graphql-tools
安装使用的版本不同时,也会出现这种情况。
我发现您可以通过以下任一方法更正此问题:
更改项目
package.json
文件中graphql
的版本,使其与graphql-tools
在其package.json
文件中所依赖的完全匹配。删除
graphql
作为依赖项并仅安装graphql-tools
。然后您将自动收到graphql-tools
安装的任何graphql
模块版本(只要您不依赖于安装另一个冲突版本的任何其他包)。
在其他情况下,您可能拥有正确的版本,但可能会安装多次。您可以使用 npm ls graphql
查看所有已安装的版本。尝试 运行 npm dedupe
删除重复安装。
我的问题是 .js
和 .mjs
graphql 文件由于错误的 webpack 配置而被解决。
根本原因:
来自 TypeMapper.mjs
文件 graphql-compose
,import 语句没有文件扩展名,这是 webpack 包失败的原因。为了解决这个问题,我需要将 fullySpecified:false
添加到 webpack 配置中。
{
test: /\.m?js/,
include: /node_modules/,
type: "javascript/auto",
resolve: {
fullySpecified: false
}
}
而且我还修改了 resolve 语句,比如
resolve: {
extensions: [".ts", ".js", ".mjs"] // that was the actual problem
}
由于 fullySpecified
配置已设置为 false
,webpack 尝试根据 resolve.extentions
配置的顺序解析没有扩展名的文件。由于该配置中的错误顺序,graphql
分机号为 .js
的文件正在解析,尽管所有其他文件都使用 .mjs
分机号。
解法:
只需将 resolve.extensions
配置重新排序为
resolve: {
extensions: [".ts", ".mjs", ".js"]
}
当我的 .npmrc 没有正确的条目(例如用户名和密码)时,我得到了这个确切的错误。我们正在使用 jFrog 规范包安装。 .npmrc 应该位于具有适当条目的根目录下。 例如:有效的 .npmrc 文件
@<company-name>:registry=<registry-url>
//<artifactory-name>:_password=${PASSWORD}
//<artifactory-name>:username=${JFROG_USERNAME}
//<artifactory-name>:email=${YOUR_EMAIL}
//<artifactory-name>:always-auth=true
在我的例子中,我将 webpack-node-externals
库添加到 webpack 配置中,并且我能够 运行 我的捆绑应用程序。
externalsPresets: { node: true },
externals: [nodeExternals()],
我正在使用 webpack 版本 5.*
我也在使用 yarn
包管理器,所以我在 package.json
resolutions
"resolutions": {
"graphql": "^15.3.0"
}