简体   繁体   中英

Gatsby GraphQL Regex Query Multiple Images

I have images that I want to query for in Gatsby from multiple blog posts. The file naming for each is composed of the following manner.

  • blog-post1-preview.jpg
  • blog-post2-preview.jpg
  • blog-post3-preview.jpg
  • blog-post1.jpg

I want to select only the image that contain-preview.jpg in their name, but I want to select all of them and loop through at a later point. Currently I have a graphql query that selects only one of them using regex.

query MyQuery {
  file(absolutePath: {regex: "/preview.jpg$/"}) {
    childrenImageSharp {
      fluid {
        originalName
      }
    }
  }
}

However, this only returns one of the images that contain preview.jpg in their name. In the example above, it would return blog-post3-preview.jpg but not 1 or 2. How can I have all 3 be returned?

I've found the solution, my original regex worked fine. However, I needed to use a different query path other than file() as this only returns one node in the file system it seems.

Here's the query that worked for me.

query MyQuery {
  allImageSharp(filter: {fluid: {originalName: {regex: "/preview.jpg$/"}}}) {
    edges {
      node {
        fluid {
          originalName
          originalImg
        }
      }
    }
  }
}

I found the solution here at How do I query multiple images with gatsby-image?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM