通过Graphql成功地查询Contentful,以便在我的gatsby项目中访问和显示图像

 

使用gatsby-image(v2)

import * as React from "react"
import { graphql } from "gatsby"
import Img from "gatsby-image"

function MainApp( {data} ){
return(
<h1>
{data.allContentfulHeroImage.edges.map(({ node },index) => <Img fluid={node.heroImage.fluid} alt = {``} key = {``} />)}
</h1>
)
}

export default MainApp;

export const query = graphql`
query MyQuery {
allContentfulHeroImage { 
edges {
node {
heroImage {
fluid (maxWidth:2000){
...GatsbyContentfulFluid
}
}
}
}
}
}`

使用gatsby-plugin-image(v3)

import * as React from "react"
import { graphql } from "gatsby"
import { GatsbyImage } from "gatsby-plugin-image"


function MainApp( {data} ) {

  return (
    <>  
      {data.allContentfulHeroImage.edges.map(({ node }, index) => <GatsbyImage image={node.heroImage.gatsbyImageData} alt={``} key={``} />)}
    </>
  )
}

export default MainApp;

export const query = graphql 
`query MyQuery {
  allContentfulHeroImage {
    edges {
      node {
        heroImage {
           gatsbyImageData(
             width: 200
             placeholder: BLURRED
             formats: [AUTO, WEBP, AVIF]
           )
          }
        }
      }
    }
  }
}`

评论

此博客中的热门博文

Oracle忘了保存,Oracle Cloud甲骨文云服务器忘记SSH秘钥或未设置Public key的解决办法...

Nginx【Docker系列】一个反向代理神器——Nginx Proxy Manager