问题描述
我在Strapi上的项目内容类型上设置了一个名为图片"的多媒体(图像)字段,并添加了2个项目,每个图片包含4张图像.我想使用Graphql在盖茨比中查询这些图像.
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(node)
和 console.log(image)
在第二个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中查询多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!