使用graphql和gatsby从strapi查询多个图像
我正试图将一些图像绘制到图像库中。每个ID应该有多个图像,但在graphIQL中只返回1个 我找到了,但我无法计算应该在我的盖茨比节点文件中放置什么。任何帮助都将不胜感激 graphIQL-使用graphql和gatsby从strapi查询多个图像,gatsby,strapi,graphiql,Gatsby,Strapi,Graphiql,我正试图将一些图像绘制到图像库中。每个ID应该有多个图像,但在graphIQL中只返回1个 我找到了,但我无法计算应该在我的盖茨比节点文件中放置什么。任何帮助都将不胜感激 graphIQL- 正如我在另一个答案中所建议的那样,如果循环正在打印相同的图像,则在内部,没有从Strapi正确设置id。您可以自定义GraphQL节点模式以添加自定义参数,以便使用Gatsby提供的不同API绕过此限制,createRemoteFileNode应符合您的要求 const { createRemoteFile
正如我在另一个答案中所建议的那样,如果循环正在打印相同的图像,则在内部,没有从Strapi正确设置id。您可以自定义GraphQL节点模式以添加自定义参数,以便使用Gatsby提供的不同API绕过此限制,
createRemoteFileNode
应符合您的要求
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;
}
}
}
}
};
根据您的数据结构,您可能需要更改循环,在这种情况下,图像位于类别
节点内,因此必须通过嵌套两个不同的循环来推断
我们的想法是循环遍历所有图像节点,并添加localFile\uuuuuu节点
字段,其中包含:
image.localFile___NODE = fileNode.id;
id
以前是在以下位置创建的:
createNodeId: (id) => image.id.toString(),