[英]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.
错误:无法从另一个模块或领域使用 GraphQLSchema“[object GraphQLSchema]”。
Ensure that there is only one instance of "graphql" in the node_modules directory.
确保 node_modules 目录中只有一个“graphql”实例。 If different versions of "graphql" are the dependencies of other relied on modules, use "resolutions" to ensure only one version is installed.
如果不同版本的“graphql”是其他依赖模块的依赖,使用“resolutions”确保只安装一个版本。
What's going on?这是怎么回事?
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
.当您安装的
graphql
模块的版本与graphql-tools
安装和使用的版本不同时,也可能出现这种情况。
I have found you can correct this by either:我发现您可以通过以下任一方式纠正此问题:
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.在项目的
package.json
文件中更改graphql
的版本,以完全匹配graphql-tools
在其package.json
文件中所依赖的内容。
Removing graphql
as a dependency and just installing graphql-tools
.删除
graphql
作为依赖项并仅安装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).然后,您将自动收到
graphql-tools
安装的任何graphql
模块版本(只要您不依赖任何其他安装另一个冲突版本的软件包)。
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.您可以使用
npm ls graphql
查看所有已安装的版本。 Try running npm dedupe
to remove duplicate installations.尝试运行
npm dedupe
以删除重复安装。
This happens because graphql-tools
module imports graphql
from its CommonJS module, while my code does it from the ES module.发生这种情况是因为
graphql-tools
模块从其 CommonJS 模块导入graphql
,而我的代码是从 ES 模块导入的。 That is, each object in my own module comes from the ES module, while graph-tool
's not.也就是说,我自己模块中的每个对象都来自 ES 模块,而
graph-tool
不是。
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:这就像从
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)
My problem was both .js
an .mjs
graphql files are resolved due to wrong webpack configuration.我的问题是
.js
和.mjs
graphql 文件都由于错误的 webpack 配置而得到解决。
Root cause:根本原因:
From TypeMapper.mjs
file in graphql-compose
, import statement does not have file extension and that was a failure on webpack bundle.在
graphql-compose
中的TypeMapper.mjs
文件中,import 语句没有文件扩展名,这在 webpack 包上是失败的。 In order to solve it, I required to add fullySpecified:false
into the webpack config.为了解决这个问题,我需要在 webpack 配置中添加
fullySpecified:false
。
{
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.由于
fullySpecified
配置已设置为false
,webpack 试图按照resolve.extentions
配置的顺序来解析没有扩展名的文件。 Due to the wrong order in that config, graphql
files with .js
ext were been resolving although all other files were using .mjs
one.由于该配置中的错误顺序,尽管所有其他文件都使用
.mjs
文件,但带有.js
ext 的graphql
文件正在解析。
Solution:解决方案:
Simply re-order resolve.extensions
config as只需将
resolve.extensions
配置重新排序为
resolve: {
extensions: [".ts", ".mjs", ".js"]
}
In my case, I added the webpack-node-externals
library to the webpack configuration, and I was able to run my bundled application.在我的例子中,我将
webpack-node-externals
库添加到 webpack 配置中,并且能够运行我的捆绑应用程序。
externalsPresets: { node: true },
externals: [nodeExternals()],
I am using webpack version 5.*我正在使用 webpack 版本 5.*
Also I am using yarn
package manager, so I added resolutions
in my package.json
我也在使用
yarn
包管理器,所以我在package.json
中添加了resolutions
"resolutions": {
"graphql": "^15.3.0"
}
I got this exact error when my .npmrc did not have proper entries such as username and password.当我的 .npmrc 没有正确的条目(例如用户名和密码)时,我得到了这个确切的错误。 We are using jFrog to normalise package installation.
我们正在使用 jFrog 来规范包安装。 .npmrc should be located at root with proper entries.
.npmrc 应位于具有正确条目的根目录。 ex: .npmrc file which works
例如:有效的 .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
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.我还删除
node_modules
和package-lock.json
并使用 yarn 而不是 npm 安装了包。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.