如何在 reactJS 中映射嵌套的 graphQL 查询

Posted

技术标签:

【中文标题】如何在 reactJS 中映射嵌套的 graphQL 查询【英文标题】:How to map a nested graphQL query in reactJS 【发布时间】:2021-02-14 00:33:27 【问题描述】:

我想从这个响应中显示 featuresImage -> mediaItemUrl 我可以映射 authorId 和 title 东西,但是如何获取图像的媒体 URL

const ALL_BLOGS = gql`
 
  posts(last: 10) 
    nodes 
      authorId
      guid
      title
      commentCount
      commentStatus
      author 
        node 
          name
        
      
      date
      pinged
      uri
      featuredImage 
        node 
          mediaItemUrl
        
      
    
  

这里是 ReactJS 代码The code snippet

【问题讨论】:

【参考方案1】:

在您的 sn-p 中,您似乎忘记将 node 放在 mediaItemUrl 之前

data.blog.featuredImage.map(featuredImage => (
             <li key=featuredImage.node>
                  <h2>featuredImage.node.mediaItemUrl</h2>
             </li>
))

【讨论】:

以上是关于如何在 reactJS 中映射嵌套的 graphQL 查询的主要内容,如果未能解决你的问题,请参考以下文章