reactjs - Gatsby Graph-ql选择子字段错误

标签 reactjs graphql gatsby

我正在尝试使用 Gatsby 制作自己的网站。第一次使用graph-ql,我使用Gatsby提供的graph-ql ide进行查询以收集SiteMeatadata

  const data = useStaticQuery(graphql`
    query SiteInformationQuery {
      __typename
      site {
        siteMetadata {
          author {
            name
            summary
          }
          title
          description
        }
      }
    }
  `);

我预测它会返回

{
  "data": {
    "site": {
      "siteMetadata": {
        "title": "Gatsby Typewriter theme",
        "author": {
          "name": "dodok8",
          "summary" : "student"
        }
      }
    }
  }
}

但它犯了一个错误。

 ERROR #85901  GRAPHQL

There was an error in your GraphQL query:

Field "author" of type "SiteSiteMetadataAuthor" must have a selection of subfields. Did you mean "author { ... }"?

File: src\components\seo.js:21:13

但是我在查询中配置了 author 的所有子文件。我的子字段和查询语句有什么问题?

它是有关存储在 gatsby-config.js 中的 siteMetadata 的原始数据。

module.exports = {
  siteMetadata: {
    title: `Gatsby Typewriter theme`,
    description: `Demo page of Gatsby Typewriter theme`,
    author: {
      name: 'dodok8',
      summary: 'student',
    },
  },

最佳答案

问题不在于我编写的查询,而是 componets/seo.js 中的基本查询。如果不编辑此文件,第 13 行会有一个查询,

    graphql`
      query {
        site {
          siteMetadata {
            title
            description
            author
          }
        }
      }
    `
  );

就我而言,我向作者添加了子字段,因此我应该将其更改为

    graphql`
      query {
        site {
          siteMetadata {
            title
            description
            author {
              name
              summary
            }
          }
        }
      }
    `
  );

关于reactjs - Gatsby Graph-ql选择子字段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61024666/

相关文章:

javascript - React 子组件中的处理程序未按预期工作

graphql-cli 输出不会产生@unique

如果过滤器变量为空,GraphQL 禁用过滤

javascript - 这个 fetch 在 React gatsby 网站上是如何优化的

reactjs - 包含 Github Actions 的 env 文件

reactjs - 无法使用 universal-cookie 从我的 React 应用程序访问带有 HttpOnly 标志的 cookie

reactjs - React redux 不会改变路线

javascript - 使用 Apollo 客户端进行 useMutation 后未触发 useEffect

java - 过滤嵌套字段,或将类型映射到查询

reactjs - 为什么我们需要 Gatsby 项目文件结构中的模板文件夹