【问题标题】:Error: Cannot use GraphQLObjectType "__Directive" from another module or realm错误:无法使用来自另一个模块或领域的 GraphQLObjectType“__Directive”
【发布时间】:2019-07-27 09:45:37
【问题描述】:

我正在尝试使用 Gatsby 从 GraphQL 查询数据。我按照Gatsby's 使用 GraphQL 查询页面中的数据的说明进行操作,但一直出错。

错误(这是我需要解决的比下面的示例更多的问题):

错误:无法从另一个模块使用 GraphQLObjectType "__Directive" 或领域。确保只有一个“graphql”实例 node_modules 目录。如果不同版本的“graphql”是 其他依赖模块的依赖,使用“决议” 确保只安装一个版本。
https://yarnpkg.com/en/docs/selective-version-resolutions Duplicate “graphql”模块不能同时使用,因为不同
版本可能具有不同的功能和行为。数据来自 在另一个函数中使用的一个版本可以产生 令人困惑和虚假的结果。

知道为什么会这样吗?我的 node_modules 文件夹中只有一个 graphql 实例

我正在使用的示例:

我打算从 GraphQL 导入我自己的数据,但现在作为概念证明,我正在研究这个 Gatsby Rick and Morty 示例(上面的链接 - 我还没有使用 axios 演示)。

index.js 代码:

import React, { Component } from 'react'
import { graphql } from 'gatsby'

// This query is executed at build time by Gatsby.
export const GatsbyQuery = graphql`
  {
    rickAndMorty {
      character(id: 1) {
        name
        image
      }
    }
  }
`

class ClientFetchingExample extends Component {
  render() {
    const {
      rickAndMorty: { character },
    } = this.props.data

    return (
      <div style={{ textAlign: "center", width: "600px", margin: "50px auto" }}>
        <h1>{character.name} With His Pupper</h1>
        <p>Rick & Morty API data loads at build time.</p>
        <div>
          <img
            src={character.image}
            alt={character.name}
            style={{ width: 300 }}
          />
        </div>
        <h2>Image of Rick's pupper</h2>
        <p>This will come from a request on the client</p>
      </div>
    )
  }
}

export default ClientFetchingExample

gatsby-config.js:

plugins: [
  {
    resolve: "gatsby-source-graphql",
    options: {
      typeName: "RMAPI",
      fieldName: "rickAndMorty",
      url: "https://rickandmortyapi-gql.now.sh/",
    },
  },
...

【问题讨论】:

    标签: graphql gatsby


    【解决方案1】:

    我检查了https://rickandmortyapi-gql.now.sh/,发现您的查询有误。应该是这样的:

    export const GatsbyQuery = graphql`
      query rickAndMorty {
        character(id: 1) {
          name
          image
        }
      }
    `
    

    我猜你试图进行命名查询,但创建错误。

    plugins: [
      {
        resolve: "gatsby-source-graphql",
        options: {
          typeName: "RMAPI",
          fieldName: "character",
          url: "https://rickandmortyapi-gql.now.sh/",
        },
      },
    

    ...

    【讨论】:

    • 我试过了,但都返回错误:7:5 error Cannot query field "rickAndMorty" on type "Query"
    【解决方案2】:

    我的问题是关于 错误 而不是代码(我根据上下文请求添加了代码)。错误已解决,说明如下。

    我首先检查我的yarn.lock 是否有graphql 实例。 (我有两个版本的graphql)。错误说应该只有一个。

    修复它:

    1) 我在 package.json 中添加了以下内容:

    "resolutions": {
      "graphql": "^14.1.0"
    }
    

    2) 我跑了npm install

    这为我解决了错误,希望对其他人有所帮助。


    旁注:在终端中运行find node_modules -name graphql 来查找您的node_modules 中可能拥有的版本的另一种方法。

    对我来说,它返回了两个实例(不确定 gatsby 来自哪里):

    node_modules/graphql
    node_modules/gatsby/node_modules/graphql
    

    要查找版本,请使用grep version node_modules/graphql/package.json

    这就是我发现我正在运行 GraphQL 14.1.0

    【讨论】:

      猜你喜欢
      • 2021-01-05
      • 2019-03-09
      • 1970-01-01
      • 2020-12-31
      • 2020-01-11
      • 1970-01-01
      • 1970-01-01
      • 2017-04-22
      • 1970-01-01
      相关资源
      最近更新 更多