使用带有gatsby的imageSharp正则表达式在graphQL中出错

问题描述 投票:1回答:1

我正在使用gatsby创建一个简单的博客。当我尝试搜索特定图像时,我从graphql中收到错误。我有以下配置:

安装"gatsby-image": "^1.0.55"

graphql`
      query MainLayoutQuery {
        heroImage: imageSharp(id: { regex: "/hero.jpg/" }) {
          id
          sizes(quality: 100) {
            base64
            tracedSVG
            aspectRatio
            src
            srcSet
            srcWebp
            srcSetWebp
            sizes
            originalImg
            originalName
          }
        }
      }
    `

当我在graphql ui中运行该查询时,我得到:

{
  "errors": [
    {
      "message": "Cannot read property 'id' of undefined",
      "locations": [
        {
          "line": 31,
          "column": 3
        }
      ],
      "path": [
        "heroImage"
      ]
    }
  ],
  "data": {
    "heroImage": null
  }
}

但是,如果我在没有正则表达式的情况下运行相同的查询,它可以正常工作:

{
  heroImage: imageSharp {
    id
    sizes(quality: 100) {
      base64
      tracedSVG
      aspectRatio
      src
      srcSet
      srcWebp
      srcSetWebp
      sizes
      originalImg
      originalName
    }
  }
}

当然,它带来了它可以访问的第一个图像

"data": {
    "heroImage": {
      "id": "/Users/marcosrios/dev/workspace/atravesando-todo-limite/src/posts/2018-08-25-tengo-miedo/cover.png absPath of file >> ImageSharp"
    }
}
regex graphql gatsby
1个回答
5
投票

您使用哪个版本的盖茨比?如果v2你需要编辑你的查询,因为有变化:https://next.gatsbyjs.org/docs/migrating-from-v1-to-v2/#dont-query-nodes-by-id

您的查询将如下所示:

graphql`
      query MainLayoutQuery {
        heroImage: imageSharp(fluid: { originalName: { regex: "/hero.jpg/" } }) {
          id
          fluid(quality: 100) {
            base64
            tracedSVG
            aspectRatio
            src
            srcSet
            srcWebp
            srcSetWebp
            sizes
            originalImg
            originalName
          }
        }
      }
    `
© www.soinside.com 2019 - 2024. All rights reserved.