Gatsby GraphQL query for multiple images

浪子不回头ぞ 提交于 2020-06-11 03:40:08

问题


I'm stuggling to figure out how to query for multiple specific images with GraphQL in Gatsbyjs. My initial thought was to do something like this:

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

This throws an error in 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
        }
      ]
    }
  ]
}

Querying for one specific file (image) works fine:

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

Any suggesting of what I'm doing wrong here? Thanks :)


回答1:


Found out the trick is to use aliases as described in the graphQL docs

In my case changing the query to this seems to do the trick:

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


来源:https://stackoverflow.com/questions/48130697/gatsby-graphql-query-for-multiple-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!