Gatsby GraphQL 查询多个图像

IT技术 javascript reactjs graphql gatsby
2021-04-29 06:49:47

我正在努力弄清楚如何在 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个回答

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

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

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