【问题标题】:Getting error Field "cover" must not have a selection since type "String" has no subfields when deploy获取错误字段“cover”不能有选择,因为类型“String”在部署时没有子字段
【发布时间】:2021-10-23 23:35:31
【问题描述】:

在 netlify 上部署时出现错误:

10:49:33 AM:错误您的 GraphQL 查询出现错误:10:49:33 AM:字段“cover”不能有选择,因为类型“String”没有 子字段。上午 10 点 49 分 33 秒:这可能会发生,例如不小心添加了 { } 到“封面”字段。如果你没想到“封面”是类型 “字符串”确保您的输入源和/或插件是正确的。 从组件中提取查询失败 - 0.355 秒

这是我的以下代码:

export const pageQuery = graphql`
  {
    hero: allMarkdownRemark(filter: { fileAbsolutePath: { regex: "/hero/" } }) {
      edges {
        node {
          frontmatter {
            title
            name
            subtitle
            buttonText
          }
          html
        }
      }
    }
    about: allMarkdownRemark(filter: { fileAbsolutePath: { regex: "/about/" } }) {
      edges {
        node {
          frontmatter {
            title
            avatar {
              childImageSharp {
                fluid(maxWidth: 700, quality: 90, traceSVG: { color: "#64ffda" }) {
                  ...GatsbyImageSharpFluid_withWebp_tracedSVG
                  base64
                }
              }
            }
            skills
          }
          html
        }
      }
    }
    jobs: allMarkdownRemark(
      filter: { fileAbsolutePath: { regex: "/jobs/" } }
      sort: { fields: [frontmatter___date], order: DESC }
    ) {
      edges {
        node {
          frontmatter {
            title
            company
            location
            range
            url
          }
          html
        }
      }
    }
    featured: allMarkdownRemark(
      filter: { fileAbsolutePath: { regex: "/featured/" } }
      sort: { fields: [frontmatter___date], order: DESC }
    ) {
      edges {
        node {
          frontmatter {
            title
            cover {
              childImageSharp {
                fluid(maxWidth: 700, quality: 90, traceSVG: { color: "#64ffda" }) {
                  ...GatsbyImageSharpFluid_withWebp_tracedSVG
                  base64
                }
              }
            }
            tech
            github
            external
          }
          html
        }
      }
    }
    projects: allMarkdownRemark(
      filter: {
        fileAbsolutePath: { regex: "/projects/" }
        frontmatter: { showInProjects: { ne: false } }
      }
      sort: { fields: [frontmatter___date], order: DESC }
    ) {
      edges {
        node {
          frontmatter {
            title
            tech
            github
            external
          }
          html
        }
      }
    }
    contact: allMarkdownRemark(filter: { fileAbsolutePath: { regex: "/contact/" } }) {
      edges {
        node {
          frontmatter {
            title
            buttonText
          }
          html
        }
      }
    }
  }
`;

我尝试像人们在尝试搜索解决方案时所说的那样添加 base64,但它仍然给我这个错误。我该如何解决?

【问题讨论】:

  • 是否在本地构建?

标签: graphql gatsby gatsby-plugin


【解决方案1】:
frontmatter {
        title
        cover {
          childImageSharp {
            fluid(maxWidth: 700, quality: 90, traceSVG: { color: "#64ffda" }) {
              ...GatsbyImageSharpFluid_withWebp_tracedSVG
              base64
            }
          }
        }
        tech
        github
        external
      }

在这段代码 sn-p 中,字段 coverString 类型,而不是 object。在 graphql 中,您无法对 int、string、id 等原始数据类型进行子选择。因此您无法在封面中访问 childImageSharp。检查 API 的结构或从后端修复它。这里,

frontmatter {
    title
    cover 
    tech
    github
    external
  }

这会起作用

【讨论】:

  • 它给了我这个错误Field "cover" of type "File" must have a selection of subfields. Did you mean "cover { ... }"?
  • 但是在上面的错误中,它说“cover”是字符串类型,但现在它是文件。如果那是罐头,那么封面是一个对象,它必须像原始代码一样具有子选择
  • 在sn-p上面cover是一个object
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-17
  • 2021-03-08
  • 2021-02-17
  • 2019-10-03
  • 2020-05-01
  • 2021-10-03
  • 1970-01-01
相关资源
最近更新 更多