Javascript 错误:无法使用GraphQLSchema";[object GraphQLSchema]”;来自另一个模块或领域
给定以下代码:Javascript 错误:无法使用GraphQLSchema";[object GraphQLSchema]”;来自另一个模块或领域,javascript,node.js,ecmascript-6,graphql,graphql-js,Javascript,Node.js,Ecmascript 6,Graphql,Graphql Js,给定以下代码: 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 ({
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)
我得到这个错误:
错误:无法从其他对象使用GraphQLSchema“[object GraphQLSchema]”
模块或领域
确保中只有一个“graphql”实例
节点\模块目录。如果使用不同版本的“graphql”
其他依赖模块的依赖关系,使用“解决方案”确保
只安装了一个版本
发生了什么?发生这种情况是因为
graphql工具
模块从其CommonJS模块导入graphql
,而我的代码从ES模块导入。也就是说,我自己模块中的每个对象都来自ES模块,而图形工具
不是
解决方案
从graphql
导入CommonJS模块与从graphql
和graphql工具
导入任何东西一样简单,两个对象都可以相互对话:
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工具安装和使用的版本不同时,也可能出现这种情况
我发现您可以通过以下任一方法来纠正此问题:
在项目的package.json
文件中更改graphql
的版本,以精确匹配graphql工具在其package.json
文件中所依赖的内容
作为依赖项删除graphql
,只需安装graphql工具
。然后,您将自动收到graphql工具
安装的任何graphql
模块版本(只要您不依赖于安装另一个冲突版本的任何其他软件包)
在其他情况下,您可能有正确的版本,但可能会安装多次。您可以使用npm ls graphql
查看所有安装的版本。尝试运行npm duplicate
删除重复安装。我的问题是.js
和.mjs
由于网页包配置错误,graphql文件已解决
根本原因:
从graphql compose
中的TypeMapper.mjs
文件中,导入语句没有文件扩展名,这是webpack bundle上的一个故障。为了解决这个问题,我需要在网页配置中添加fullysspecified:false
{
测试:/\.m?js/,
包括:/node\u模块/,
键入:“javascript/auto”,
决心:{
完全指定:false
}
}
我还修改了resolve语句,比如
解析:{
扩展:[“.ts”、“.js”、“.mjs”]//这就是实际问题所在
}
由于fullysspecified
config已设置为false
,webpack尝试解析文件,但没有扩展名,顺序为resolve.extensions
config。由于该配置中的顺序错误,graphql
文件和.js
ext正在解析,尽管所有其他文件都在使用.mjs
one
解决方案:
只需重新排序resolve.extensions
config as
解析:{
扩展名:[“.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
@:注册表=
//:_password=${password}
//:username=${JFROG\u username}
//:email=${YOUR_email}
//:always auth=true
这对我很有效,ls
命令为我提供了上下文,但重复数据消除
将其清理干净。