首页 文章

Gatsby GraphQL查询多个图像

提问于
浏览
3

我正在努力弄清楚如何在Gatsbyjs中使用GraphQL查询多个特定图像 . 我最初的想法是做这样的事情:

file(relativePath: {eq: "images/front.jpg"}) {
  id
}
file(relativePath: {eq: "images/front2.jpg"}) {
  id
}

这会在GraphQL中引发错误:

{
  "errors": [
    {
      "message": "Fields \"file\" conflict because they have differing arguments. Use different aliases on the fields to fetch both if this was intentional.",
      "locations": [
        {
          "line": 28,
          "column": 1
        },
        {
          "line": 31,
          "column": 1
        }
      ]
    }
  ]
}

查询一个特定文件(图像)工作正常:

file(relativePath: {eq: "images/front.jpg"}) {
  id
}

有什么建议我在这里做错了吗?谢谢 :)

1 回答

  • 5

    发现诀窍是使用graphQL docs中描述的别名

    在我的情况下,将查询更改为此似乎可以解决问题:

    front: file(relativePath: {eq: "images/front.jpg"}) {
      id
    }
    front2: file(relativePath: {eq: "images/front2.jpg"}) {
      id
    }
    

相关问题