【问题标题】:graphQL: how to get an object from a list of objects, in a querygraphQL:如何在查询中从对象列表中获取对象
【发布时间】:2018-07-09 08:45:19
【问题描述】:

我正在尝试在 GatsbyJS 项目中使用 graphQL,但不确定如何从“recipeImages”对象中的三个列表中提取特定的“URL”对象。现在,我只能拉出 recipeImages 对象,像这样:

<img src={node.recipeImages}/>

但我希望能够访问在此查询中看到的三个单独的 URL 对象:

{
  "data": {
    "allContentfulBlogPost": {
      "edges": [
        {
          "node": {
            "id": "c1Qz3hWuPuQEIIUkSos0MEO",
            "postTitle": "Schwarzwälder Kirschtorte",
            "postDate": "2018-01-30T00:00+01:00",
            "slug": "schwarzwälder-kirschtorte",
            "methodText": {
              "childMarkdownRemark": {
                "html": "<p>This is the method text</p>"
              }
            },
            "recipeImages": [
              {
                "title": "imageOne",
                "file": {
                  "url": "//images.contentful.com/62o0h4ehxjxr/kkc57vWLPaEakYueyYqC6/c61b4641797a2fcaf3476ef9a3a24db6/image.jpg"
                }
              },
              {
                "title": "imageTwo",
                "file": {
                  "url": "//images.contentful.com/62o0h4ehxjxr/2ifxQEvnYwkaAe6e2YKISa/de2b6f62c4cac3b501fe76146b745790/image1.jpg"
                }
              },
              {
                "title": "imageThree",
                "file": {
                  "url": "//images.contentful.com/62o0h4ehxjxr/17g7ZHqrEWIgcyuye08myG/6b55386a31db2dd319148795953da7a4/image2.jpg"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

【问题讨论】:

    标签: reactjs graphql graphql-js gatsby


    【解决方案1】:

    我明白了:

    <img src={recipeImages[0].responsiveResolution.src}/> 
    

    【讨论】:

      猜你喜欢
      • 2014-01-08
      • 1970-01-01
      • 2019-08-24
      • 2019-02-06
      • 1970-01-01
      • 2019-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多