Gatsby:使用 GraphQL 查询从存储在 json 中的路径获取所有图像
上下文
我正在尝试制作一个画廊。它应该从不同场合(例如博客)显示图像。
为此,我有一个JSON文件,其中存储了所有帖子(标题,slug,date和images-folder-path)。为了显示它们,我正在使用GraphQl。
问题
我现在的问题是显示各个帖子的所有图像。在JSON文件中,仅存储图像路径。
JSON结构:
[
{
"title": "Gallery 1",
"slug": "gallery-1",
"date": "02.01.2022",
"images": "./gallery-1/"
},
{
"title": "Gallery 2",
"slug": "gallery-2",
"date": "15.01.2022",
"images": "./gallery-2/"
}
]
现在,对于画廊1 ,我想将所有图像存储在/gallery-1/文件夹等中。
query MyQuery {
allGalleriesJson {
edges {
node {
title
slug
date
images
}
}
}
}
运行此GraphQl命令时,我会得到路径的字符串。但是现在,我想用此字符串将所有图像在文件夹中获取。我该如何实现?
Context
I am trying to make a Gallery. It should display images from different occasions like a blog.
For this I have a JSON file in which all posts (title, slug, date and images-folder-path) are stored. To display them I'm using GraphQL.
Problem
My problem now is to display all images for the respective post. In the JSON file only the path to the images is stored.
JSON structure:
[
{
"title": "Gallery 1",
"slug": "gallery-1",
"date": "02.01.2022",
"images": "./gallery-1/"
},
{
"title": "Gallery 2",
"slug": "gallery-2",
"date": "15.01.2022",
"images": "./gallery-2/"
}
]
Now for Gallery 1 I would like to get all the images stored in the ./gallery-1/ folder and so on.
query MyQuery {
allGalleriesJson {
edges {
node {
title
slug
date
images
}
}
}
}
When I run this GraphQL command, I get the string of the path. But now I would like to get all images in the folder with this string. How can I achieve that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您想使用
过滤器
过滤器(非常冗余)以获取/Gallery-1/
(或每个Gallery)文件夹中的所有图像。为此,您只需要使用
上下文
在您的createPage
函数中提供上下文变量。注意:这是一个抽象,因为没有提供样板。根据您的意愿进行调整,但要为每个画廊对象传递
slug
的想法。之后,在您的模板中(
your-template-file.js
) )您需要使用提供的slug
(每个页面不同)注意:
Allgalleriesjson
可能有所不同,或者您可能有> Galleriesjson
可用。如果是这样,请使用它,指向特定节点的性能比所有这些节点都更具性能=“ https://www.gatsbyjs.com/docs/query-filters/” rel =“ nofollow noreferrer”> https://www.gatsbyjs.com/docs/docs/query-filters/
,您可以使用绝对路径(
fileabsolutepath
)或相对。在graphiql Playground(localhost:8000/___ graphql
)中测试您的查询。尽管查询或摘要可能与此示例有所不同,但请获取想法:
gatsby-node.js
+createPage
)slug
通过上下文中的模板变量allgalleriesjson
获取您的文件夹图像I think you want to use the
filter
filter (quite redundant) to get all the images in the/gallery-1/
(or each gallery) folder.To do that, you just need to provide a context variable to your template, using the
context
available in yourcreatePage
function.Note: this is an abstraction since no boilerplate has been provided. Tweak it as you wish but get the idea of passing a
slug
for each gallery object.After that, in your template (
your-template-file.js
) you'll need to filter the query using theslug
provided (different for each page)Note: the
allGalleriesJson
may be different or you may havegalleriesJson
available. If so, use it, it's more performant to point a specific node than all of themHere I'm using the
eq
(equals) filter but you have a bunch available: https://www.gatsbyjs.com/docs/query-filters/In the same way, you can use absolute paths (
fileAbsolutePath
) or relative. Test your queries in the GraphiQL playground (localhost:8000/___graphql
).Despite the query or the snippets may differ from this example, get the idea:
gatsby-node.js
+createPage
)slug
variable into the template through the contextallGalleriesJson
to get your folder images