I am making a Gatsby static website and I'm having trouble with GraphQL.
I am trying to query all images in a folder using GraphQL. My data has the path:
"images" : "./relimages/vila-franceza",
"cardimages" : "./relimages/main-page-card-images/vila-franceza.jpg",
Getting the cardimages image works properly. I can manipulate it with gatsby image however I want.
But when I query for images (which is a folder), I only get the path to the folder.
Here's my query:
query flagquery {
allDataRoJson {
edges {
node {
images
cardimages {
id
childImageSharp {
fluid {
originalImg
}
}
}
}
}
}
}
In my query, images returns as a string. How can i instead get it to be queryable by graphQL to instead get an array of images with the childImageSharp property like in the case where i specify the one image.
Here's the output of the query in GraphiQL
{
"data": {
"allDataRoJson": {
"edges": [
{
"node": {
"images": "./relimages/vila-franceza",
"cardimages": {
"id": "2a1fca39-6192-5e09-b913-43c5bf48966f",
"childImageSharp": {
"fluid": {
"originalImg": "/static/30337f0e47a254b68d443be671031647/0a89a/vila-franceza.jpg"
}
}
}
}
},
{
"node": {
"images": "./relimages/vila-franceza",
"cardimages": {
"id": "2a1fca39-6192-5e09-b913-43c5bf48966f",
"childImageSharp": {
"fluid": {
"originalImg": "/static/30337f0e47a254b68d443be671031647/0a89a/vila-franceza.jpg"
}
}
}
}
},
{
"node": {
"images": "./relimages/vila-franceza",
"cardimages": null
}
}
]
}
}
}
I am making a Gatsby static website and I'm having trouble with GraphQL.
I am trying to query all images in a folder using GraphQL. My data has the path:
"images" : "./relimages/vila-franceza",
"cardimages" : "./relimages/main-page-card-images/vila-franceza.jpg",
Getting the cardimages image works properly. I can manipulate it with gatsby image however I want.
But when I query for images (which is a folder), I only get the path to the folder.
Here's my query:
query flagquery {
allDataRoJson {
edges {
node {
images
cardimages {
id
childImageSharp {
fluid {
originalImg
}
}
}
}
}
}
}
In my query, images returns as a string. How can i instead get it to be queryable by graphQL to instead get an array of images with the childImageSharp property like in the case where i specify the one image.
Here's the output of the query in GraphiQL
{
"data": {
"allDataRoJson": {
"edges": [
{
"node": {
"images": "./relimages/vila-franceza",
"cardimages": {
"id": "2a1fca39-6192-5e09-b913-43c5bf48966f",
"childImageSharp": {
"fluid": {
"originalImg": "/static/30337f0e47a254b68d443be671031647/0a89a/vila-franceza.jpg"
}
}
}
}
},
{
"node": {
"images": "./relimages/vila-franceza",
"cardimages": {
"id": "2a1fca39-6192-5e09-b913-43c5bf48966f",
"childImageSharp": {
"fluid": {
"originalImg": "/static/30337f0e47a254b68d443be671031647/0a89a/vila-franceza.jpg"
}
}
}
}
},
{
"node": {
"images": "./relimages/vila-franceza",
"cardimages": null
}
}
]
}
}
}
Share
Improve this question
edited Nov 20, 2019 at 22:48
Link
asked Nov 20, 2019 at 15:44
LinkLink
3717 silver badges17 bronze badges
2
-
Pretty sure it is just a syntax error, maybe this should be
"images" : "src/relimages/vila-franceza", "cardimages" : "src/relimages/main-page-card-images/vila-franceza.jpg",
or"images" : "static/relimages/vila-franceza", "cardimages" : "stactic/relimages/main-page-card-images/vila-franceza.jpg,
– Nicholas Cappello Commented Nov 22, 2019 at 5:16 - Hey nick, the syntax is fine, because it works. the problem is that it sees images as just a string instead of a folder. – Link Commented Nov 22, 2019 at 8:20
1 Answer
Reset to default 6Assuming you want to get all the images in the "images" folder and output them on your page with gatsby-image, this is how I do it:
import React from "react"
import { graphql } from "gatsby"
import Img from "gatsby-image"
import Layout from "../ponents/layout"
export default ({ data }) => (
<Layout>
{data.allFile.edges.map(({ node }, i) => (
<Img
key={i}
fluid={node.childImageSharp.fluid}
alt={node.name}
/>
))}
</Layout>
)
export const query = graphql`
query {
allFile(
filter: {
extension: { regex: "/(jpg)|(png)|(tif)|(tiff)|(webp)|(jpeg)/" }
absolutePath: { regex: "/images/" }
}
) {
edges {
node {
name
childImageSharp {
fluid(maxWidth: 915, quality: 70) {
aspectRatio
...GatsbyImageSharpFluid_withWebp
}
}
}
}
}
}
`
My images folder was located: src/images