Gatsby:使用 GraphQL 查询从存储在 json 中的路径获取所有图像

发布于 2025-01-17 22:38:34 字数 754 浏览 0 评论 0原文

上下文

我正在尝试制作一个画廊。它应该从不同场合(例如博客)显示图像。

为此,我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

面犯桃花 2025-01-24 22:38:34

我认为您想使用过滤器过滤器(非常冗余)以获取/Gallery-1/(或每个Gallery)文件夹中的所有图像。

为此,您只需要使用上下文在您的createPage函数中提供上下文变量。

createPage({
  path: node.slug,
  component: path.resolve(`./src/templates/your-template-file.js`),
  context: {
    slug: node.slug,
  },
})

注意:这是一个抽象,因为没有提供样板。根据您的意愿进行调整,但要为每个画廊对象传递slug的想法。

之后,在您的模板中(your-template-file.js) )您需要使用提供的slug(每个页面不同)

export const query = graphql`
  query TemplateQuery($slug: String!) {
    allGalleriesJson(filter: {fileAbsolutePath: {eq: $slug  }}) {
      edges {
        node {
          title
          slug
          date
          images
        }
      }
    }

注意: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)中测试您的查询。

尽管查询或摘要可能与此示例有所不同,但请获取想法:

  • 为每个JSON对象位置创建页面(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 your createPage function.

createPage({
  path: node.slug,
  component: path.resolve(`./src/templates/your-template-file.js`),
  context: {
    slug: node.slug,
  },
})

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 the slug provided (different for each page)

export const query = graphql`
  query TemplateQuery($slug: String!) {
    allGalleriesJson(filter: {fileAbsolutePath: {eq: $slug  }}) {
      edges {
        node {
          title
          slug
          date
          images
        }
      }
    }

Note: the allGalleriesJson may be different or you may have galleriesJson available. If so, use it, it's more performant to point a specific node than all of them

Here 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:

  • Create pages for each JSON object position (gatsby-node.js + createPage)
  • Pass the slug variable into the template through the context
  • Use the context to filter the allGalleriesJson to get your folder images
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文