【问题标题】:Gatsby Graph-ql selection of subfields errorGatsby Graph-ql 子域选择错误
【发布时间】:2020-07-16 09:03:24
【问题描述】:

我很想用 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',
    },
  },

【问题讨论】:

    标签: reactjs graphql gatsby


    【解决方案1】:

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

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

    在我的例子中,我向作者添加了子字段,所以我应该像这样更改它

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

    【讨论】:

      猜你喜欢
      • 2021-10-31
      • 2021-09-05
      • 2022-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多