【发布时间】: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/",
},
},
...
【问题讨论】: