【问题标题】:Gatsby GraphQL query for multiple imagesGatsby GraphQL 查询多个图像
【发布时间】:2018-06-16 06:36:03
【问题描述】:

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

有什么暗示我在这里做错了吗?谢谢:)

【问题讨论】:

    标签: javascript reactjs graphql gatsby


    【解决方案1】:

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

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

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

    【讨论】:

    • 就像在 GraphQL 文档中提到的那样,将它们包装成一个对象会更好
    猜你喜欢
    • 2021-05-29
    • 2021-08-14
    • 2021-04-13
    • 2020-10-25
    • 2020-08-17
    • 2021-07-16
    • 2021-08-18
    • 2020-03-23
    • 2019-10-19
    相关资源
    最近更新 更多