如何使用Graphql从Strapi查询Gatsby中的多个图像
我在Strapi上的项目内容类型上设置了一个名为pictures的multiple mediaimages字段,并添加了两个项目,每个项目的图片包含4张图片。 我想使用Graphql在盖茨比中查询这些图像 这是我在gatsby-config.js中的插件数组如何使用Graphql从Strapi查询Gatsby中的多个图像,graphql,gatsby,strapi,Graphql,Gatsby,Strapi,我在Strapi上的项目内容类型上设置了一个名为pictures的multiple mediaimages字段,并添加了两个项目,每个项目的图片包含4张图片。 我想使用Graphql在盖茨比中查询这些图像 这是我在gatsby-config.js中的插件数组 plugins: [ `gatsby-plugin-react-helmet`, { resolve: `gatsby-source-filesystem`, options: {
plugins: [
`gatsby-plugin-react-helmet`,
{
resolve: `gatsby-source-filesystem`,
options: {
name: `images`,
path: `${__dirname}/src/images`,
},
},
`gatsby-plugin-sharp`,
`gatsby-transformer-sharp`,
{
resolve: `gatsby-plugin-manifest`,
options: {
name: `gatsby-starter-default`,
short_name: `starter`,
start_url: `/`,
background_color: `#663399`,
theme_color: `#663399`,
display: `minimal-ui`,
icon: `src/images/gatsby-icon.png`,
},
},
{
resolve: `gatsby-source-strapi`,
options: {
apiURL: `http://localhost:1337`,
queryLimit: 1000,
contentTypes: [`project`],
},
}]
这是我对localhost:8000/\uuuuuuuu graphql的graphql查询
query MyQuery {
allStrapiProject {
nodes {
pictures {
formats {
thumbnail {
childImageSharp {
fluid {
src
}
}
}
}
}
}
}
}
这就是我得到的结果
{
"data": {
"allStrapiProject": {
"nodes": [
{
"pictures": [
{
"formats": {
"thumbnail": null
}
},
{
"formats": {
"thumbnail": {
"childImageSharp": {
"fluid": {
"src": "/static/eb8a7ee6108ecc0e6185aced82c3316b/b4216/167f320a448c2d6ff65acf179ee627e2.jpg"
}
}
}
}
},
{
"formats": {
"thumbnail": null
}
},
{
"formats": {
"thumbnail": null
}
}
]
},
{
"pictures": [
{
"formats": {
"thumbnail": null
}
},
{
"formats": {
"thumbnail": null
}
},
{
"formats": {
"thumbnail": null
}
},
{
"formats": {
"thumbnail": null
}
}
]
}
]
}
}
}
除一个缩略图外,所有缩略图均包含null
我曾尝试运行“gatsby clean”,有时会让查询输出在多个位置具有相同的图像URL,即使Strapi上没有重复的图像。您需要创建一个本地文件节点 首先,您需要编辑gatsby-node.js文件
const { createRemoteFileNode } = require(`gatsby-source-filesystem`)
exports.onCreateNode = async ({
node,
actions,
store,
cache,
createNodeId,
}) => {
const { createNode } = actions
// replace ".sliderHome" for the name of multiple media in Strapi CMS
let sliderImages = node.sliderHome
// replace “StrapiHome” for your node type
if (node.internal.type === "StrapiHome") {
if (sliderImages.length > 0) {
// sliderImages.forEach(el => console.log(el))
const images = await Promise.all(
sliderImages.map(el =>
createRemoteFileNode({
url: `http://localhost:1337${el.url}`,
parentNodeId: node.id,
store,
cache,
createNode,
createNodeId,
})
)
)
sliderImages.forEach((image, i) => {
image.localFile___NODE = images[i].id
})
}
}
}
稍后重新启动盖茨比,现在这是您的查询
query MyQuery {
allStrapiProject {
nodes {
pictures {
localFile{
childImageSharp{
fluid(maxWidth: 1200){
// or for gatsby use ...GatsbyImageSharpFluid_withWebp
src
}
}
}
}
}
}
}
这为我带来了多个高质量的图像,我希望它对您有用您需要创建一个localFile\uuu节点 首先,您需要编辑gatsby-node.js文件
const { createRemoteFileNode } = require(`gatsby-source-filesystem`)
exports.onCreateNode = async ({
node,
actions,
store,
cache,
createNodeId,
}) => {
const { createNode } = actions
// replace ".sliderHome" for the name of multiple media in Strapi CMS
let sliderImages = node.sliderHome
// replace “StrapiHome” for your node type
if (node.internal.type === "StrapiHome") {
if (sliderImages.length > 0) {
// sliderImages.forEach(el => console.log(el))
const images = await Promise.all(
sliderImages.map(el =>
createRemoteFileNode({
url: `http://localhost:1337${el.url}`,
parentNodeId: node.id,
store,
cache,
createNode,
createNodeId,
})
)
)
sliderImages.forEach((image, i) => {
image.localFile___NODE = images[i].id
})
}
}
}
稍后重新启动盖茨比,现在这是您的查询
query MyQuery {
allStrapiProject {
nodes {
pictures {
localFile{
childImageSharp{
fluid(maxWidth: 1200){
// or for gatsby use ...GatsbyImageSharpFluid_withWebp
src
}
}
}
}
}
}
}
这为我带来了多幅高质量的图像,我希望它对您有用请尝试以下内容,替换您需要显示的值: 这里我是用户化身的示例
query MyQuery {
allStrapiUser {
edges {
node {
id
avatar {
publicURL
childImageSharp {
fluid {
src
aspectRatio
}
}
}
}
}
}
}
以及:
const poster=data.allStrapiUser.edges[0]。节点
尝试以下操作,替换需要显示的值: 这里我是用户化身的示例
query MyQuery {
allStrapiUser {
edges {
node {
id
avatar {
publicURL
childImageSharp {
fluid {
src
aspectRatio
}
}
}
}
}
}
}
以及:
const poster=data.allStrapiUser.edges[0]。节点
到目前为止,还没有官方的办法来实现这一目标。但是有一种变通方法,可以在构建过程中创建自定义节点。对于如下所示的graphql查询
query MyQuery {
allStrapiPortfolio {
edges {
node {
category {
images {
localFile {
childImageSharp {
fluid {
base64
tracedSVG
srcWebp
srcSetWebp
originalImg
originalName
}
}
}
}
}
}
}
}
}
下面给出的代码在图像之后创建localFile节点。代码应该放在gatsby-node.js中
请注意,您必须根据需要自定义代码。在我的解决方案中,由于我的数据结构,我使用了两个for循环。如果您不确定或只是想检查自定义代码是否正常工作,您只需在第一个If语句之前添加一个console.lognode,在第二个for循环语句之后添加一个console.logimage。这将为您提供有关数据结构的指示,以及您应该以何种方式进行操作。到目前为止,还没有正式的方法来实现这一点。但是有一种变通方法,可以在构建过程中创建自定义节点。对于如下所示的graphql查询
query MyQuery {
allStrapiPortfolio {
edges {
node {
category {
images {
localFile {
childImageSharp {
fluid {
base64
tracedSVG
srcWebp
srcSetWebp
originalImg
originalName
}
}
}
}
}
}
}
}
}
下面给出的代码在图像之后创建localFile节点。代码应该放在gatsby-node.js中
请注意,您必须根据需要自定义代码。在我的解决方案中,由于我的数据结构,我使用了两个for循环。如果您不确定或只是想检查自定义代码是否正常工作,您只需在第一个If语句之前添加一个console.lognode,在第二个for循环语句之后添加一个console.logimage。这应该给你一个关于你的数据结构和你应该以何种方式进行的指示