如何使用 Graphql 从 Strapi 查询 Gatsby 中的多个图像 [英] How to query multiple images in Gatsby from Strapi using Graphql
问题描述
我在 Strapi 上的项目内容类型上设置了一个称为图片的多媒体(图像)字段,并且我添加了 2 个项目,每个项目的图片包含 4 个图像.我想使用 Graphql 在 Gatsby 中查询这些图像.
I have set up a multiple media(images) field called pictures on my project content type on Strapi and I have added 2 projects with pictures containing 4 images each. I want to query these images in Gatsby using Graphql.
这是我在 gatsby-config.js 中的插件数组
This is my plugins array in gatsby-config.js
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/___graphql 上的 graphql 查询
This is my graphql query on localhost:8000/___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.
All of the thumbnails contain null except for one.
我试过运行gatsby clean",有时即使我在 Strapi 上没有重复图像,也会使查询输出在多个位置具有相同的图像 URL.
I have tried running 'gatsby clean' and sometimes get the query output to have same image urls in multiple places even though i don't have repeating images on Strapi.
推荐答案
截至目前,还没有官方"的解决方案让它发生的方法.但是有一种解决方法可以在构建过程中创建自定义节点.对于像下面这样的 graphql 查询
As of now, there is no "official" way to make it happen. But there is a workaround which creates a custom node in the build process. For a graphql query like below
query MyQuery {
allStrapiPortfolio {
edges {
node {
category {
images {
localFile {
childImageSharp {
fluid {
base64
tracedSVG
srcWebp
srcSetWebp
originalImg
originalName
}
}
}
}
}
}
}
}
}
下面给出的代码在 images
之后创建了 localFile
节点.代码应该放在 gatsby-node.js
中.
The code given below creates the localFile
node after images
. The code should go in gatsby-node.js
.
const { createRemoteFileNode } = require(`gatsby-source-filesystem`);
exports.onCreateNode = async ({ node, actions, store, cache }) => {
const { createNode, createNodeField } = actions;
if (node.internal.type !== null && node.internal.type === "StrapiPortfolio") {
for (const category of node.category) {
for (const image of category.images) {
console.log(image);
const fileNode = await createRemoteFileNode({
url: "http://localhost:1337" + image.url,
store,
cache,
createNode,
createNodeId: (id) => image.id.toString(),
});
if (fileNode) {
image.localFile___NODE = fileNode.id;
}
}
}
}
};
请注意,您必须根据需要自定义代码.在我的解决方案中,由于我的数据结构,我使用了两个 for 循环.如果您不确定或只想检查您的自定义代码是否有效,您只需在第一个 if 语句和 console.log(image) 之前添加一个
console.log(node)
在第二个 for 循环之后(在我的例子中).这应该可以让您了解您的数据结构以及您应该以何种方式继续.
Please note that you will have to customize the code depending on your needs. In my solution, I used two for loops because of my data structure. If you're unsure or just want to check if your custom code works, you can simply add a console.log(node)
before the first if statement and a console.log(image)
after the second for loop(in my case). That should give you an indication about your data structure and in which way you should proceed.
这篇关于如何使用 Graphql 从 Strapi 查询 Gatsby 中的多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!