I'm using the gatsby-image plugin to display images on my Gatsby site. In my GraphQL query, I want to be able to pass in a variable to the relativePath argument as this query is being run (in the parent component) for a number of components that require images. I cannot seem to figure out how to do this though.
This is what my query looks like:
...
const imgData = useStaticQuery(graphql`
        query{
            file(relativePath: {eq: "talent.png"}) {
                childImageSharp {
                    fixed (width: 289, height: 589) {
                        ...GatsbyImageSharpFixed
                    }
                }
            }
        }
    `)
I want to replace that "talent.png" with a variable so I can use this query across components. This is my desired query:
const imgData = useStaticQuery(graphql`
        query($pageImg: String!){
            file(relativePath: {eq: $pageImg}) {
                childImageSharp {
                    fixed (width: 289, height: 589) {
                        ...GatsbyImageSharpFixed
                    }
                }
            }
        }
    `)
I have tried adding a context to the page using onCreatePage in gatsby-node.js. This would work for a page query but the file node obviously does not recognise page context. So I tried adding a context to the file node:
module.exports.onCreateNode = ({ node, actions }) => {
    const { createNodeField } = actions
    if(node.internal.type === 'File'){
        createNodeField({
            node,
            name: 'context',
            value: {
                pageImg: node.relativePath
            }
        })
    }
}
But still I get this error:
Variable "$pageImg" of required type "String!" was not provided.
Any help in understanding how to solve this issue will be greatly appreciated.
 
    