简体   繁体   English

错误:无法使用来自另一个模块或领域的 GraphQLSchema“[object GraphQLSchema]”

[英]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:我发现您可以通过以下任一方式纠正此问题:

  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.在项目的package.json文件中更改graphql的版本,以完全匹配graphql-tools在其package.json文件中所依赖的内容。

  2. 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不是。

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:这就像从graphql导入 CommonJS 模块导入任何东西一样简单,并且来自graphqlgraphql-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_modulespackage-lock.json并使用 yarn 而不是 npm 安装了包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不能从另一个模块或领域使用 GraphQLSchema - Cannot use GraphQLSchema from another module or realm 如何从GraphQLSchema javascript对象获取.graphql文件? - how to obtain .graphql file from GraphQLSchema javascript object? 使用 GraphQLSchema,是否有模块化 GraphQLObjectTypes 的语法? - With GraphQLSchema, is there syntax to modularlize GraphQLObjectTypes? IE-graphql-SCRIPT5022:无法使用其他模块或领域中的未定义“布尔” - IE - graphql - SCRIPT5022: Cannot use undefined “Boolean” from another module or realm 错误:找不到模块“[object Object]” - Error: Cannot find module '[object Object]' 不能在模块外部使用导入语句从另一个文件调用值 - Cannot use import statement outside a module calling values from another file 我不能使用从 node.js 中的另一个模块导出的 function - I cannot use an exported function from another module in node.js 从领域对象中删除引用 - Remove reference from realm object Javascript无法实例化模块中的对象 - Javascript cannot instantiate object from module 尝试在 React 中使用“Socket.io-client”模块会引发错误:无法分配给对象“#”的只读属性“exports”<Object> &#39; - Trying to use "Socket.io-client" module in React throws error: Cannot assign to read only property 'exports' of object '#<Object>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM