【问题标题】:Error: Cannot use GraphQLSchema "[object GraphQLSchema]" from another module or realm错误:无法使用来自另一个模块或领域的 GraphQLSchema“[object GraphQLSchema]”
【发布时间】:2019-03-09 04:59:39
【问题描述】:

给定以下代码:

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”实例 node_modules 目录。如果不同版本的“graphql”是 其他依赖模块的依赖关系,使用“解决方案”来确保 只安装了一个版本。

发生了什么事?

【问题讨论】:

    标签: javascript node.js ecmascript-6 graphql graphql-js


    【解决方案1】:

    这是因为 graphql-tools 模块从其 CommonJS 模块导入 graphql,而我的代码是从 ES 模块导入的。也就是说,我自己的模块中的每个对象都来自 ES 模块,而graph-tool 不是。

    解决方案

    这就像从 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)
    

    【讨论】:

      【解决方案2】:

      当您安装的graphql模块的版本与graphql-tools安装使用的版本不同时,也可能出现这种情况。

      我发现您可以通过以下任一方式纠正此问题:

      1. 在项目的 package.json 文件中更改 graphql 的版本以完全匹配 graphql-tools 在其 package.json 文件中所依赖的内容。

      2. 删除 graphql 作为依赖项并仅安装 graphql-tools。然后您将自动收到graphql-tools 安装的任何graphql 模块版本(只要您不依赖任何其他安装另一个冲突版本的软件包)。

      在其他情况下,您可能拥有正确的版本,但可能会安装多次。您可以使用npm ls graphql 查看所有已安装的版本。尝试运行 npm dedupe 以删除重复安装。

      【讨论】:

      • 这对我有用,ls 命令给了我上下文,但dedupe 清理了它。
      【解决方案3】:

      我的问题是 .js.mjs graphql 文件都因错误的 webpack 配置而得到解决。

      根本原因:

      graphql-compose 中的TypeMapper.mjs 文件,import 语句没有文件扩展名,这是在 webpack 包上失败。为了解决这个问题,我需要在 webpack 配置中添加fullySpecified:false

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

      我还修改了类似的解析语句

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

      由于fullySpecified 配置已设置为false,webpack 试图解析没有扩展名的文件,而不是按照resolve.extentions 配置的顺序。由于该配置中的错误顺序,graphql 带有 .js ext 的文件正在解析,尽管所有其他文件都使用 .mjs 一个。

      解决方案:

      只需将resolve.extensions 配置重新排序为

      resolve: {
        extensions: [".ts", ".mjs", ".js"]
      }
      

      【讨论】:

        【解决方案4】:

        当我的 .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
        

        【讨论】:

          猜你喜欢
          • 2020-12-31
          • 2019-07-27
          • 2021-01-05
          • 2021-09-08
          • 1970-01-01
          • 1970-01-01
          • 2017-07-26
          • 2020-07-12
          • 2020-01-11
          相关资源
          最近更新 更多