Exploring the world of Gatsby and its graphQL query system for asset retrieval is a fascinating journey. I have successfully implemented a component called Image
that fetches and displays images. However, I am facing a challenge in customizing the name of the image within this component.
Let me share with you the current working component:
const Image = () => (
<StaticQuery
query={graphql`
query {
// Fetching the image gatsby-astronaut.png
placeholderImage: file(relativePath: { eq: "gatsby-astronaut.png" }) {
childImageSharp {
fluid(maxWidth: 300) {
...GatsbyImageSharpFluid
}
}
}
}
`}
render={data => <Img fluid={data.placeholderImage.childImageSharp.fluid} />}
/>
);
Now, let me show you my attempt to make the image name customizable:
const Image = ({ imgName }: { imgName: string }) => (
<StaticQuery
query={graphql`
query {
// Fetching the image based on dynamically passed imgName
placeholderImage: file(relativePath: { eq: "${imgName}.png" }) {
childImageSharp {
fluid(maxWidth: 300) {
...GatsbyImageSharpFluid
}
}
}
}
`}
render={data => <Img fluid={data.placeholderImage.childImageSharp.fluid} />}
/>
);
However, this implementation results in the following error message related to the query:
Expected 1 argument, but received 2.ts(2554)
I would greatly appreciate any guidance or insights on how to achieve a customizable image name within the Gatsby Image component.