EDIT: now with working code below
I have this query to fetch a gatsby-image:
query getImages($fileName: String) {
landscape: file(relativePath: {eq: $fileName}) {
childImageSharp {
fluid(maxWidth: 1000) {
base64
tracedSVG
aspectRatio
src
srcSet
srcWebp
srcSetWebp
sizes
originalImg
originalName
}
}
}
}
And then this query variable:
{
"fileName": "titanic.jpg"
}
The above works fine in GraphiQL.
Now I want to use it in Gatsby, so I have the following code:
import React from "react"
import { graphql } from "gatsby"
import Img from "gatsby-image"
export default ({ data }) => (
<div>
<Img fluid={data.landscape.childImageSharp.fluid} />
</div>
)
export const query = (
graphql`
query getImages($fileName: String) {
landscape: file(relativePath: {eq: $fileName}) {
childImageSharp {
fluid(maxWidth: 1000) {
base64
tracedSVG
aspectRatio
src
srcSet
srcWebp
srcSetWebp
sizes
originalImg
originalName
}
}
}
}
`,
{fileName: "knight.jpg"}
)
The above doesn't work. data.landscape.childImageSharp === null
What am I doing wrong?
EDIT:
Thanks for the help! The following code works pretty well. This post was particularly helpful. This is not an ideal solution, but it works for me.
import React from 'react';
import Img from 'gatsby-image';
import { StaticQuery, graphql } from 'gatsby';
function renderImage(file) {
return (
<Img fluid={file.node.childImageSharp.fluid} />
)
}
const MyImg = function (props) {
return <StaticQuery
query={graphql`
query {
images: allFile(filter: { sourceInstanceName: { eq: "images" } }) {
edges {
node {
extension
relativePath
childImageSharp {
fluid(maxWidth: 1000) {
...GatsbyImageSharpFluid
}
}
}
}
}
}
`}
render={(data) => {
const image = data.images.edges.find(
image => image.node.relativePath === "knight.jpg"
)
return(renderImage(image))
}}
/>
}
export default MyImg;
There are a variety of different variable data types available on Contentful's GraphQL API. As well as the standard data types such as String, Int and DateTime, you can also pass variables to a query that are entry-specific and API-specific.
When you're passing arguments in code, it's generally better to avoid constructing the whole query string yourself. Instead, you can use $ syntax to define variables in your query, and pass the variables as a separate map. . then(data => console.
The GraphiQL client allows you to create variables for use in your queries. To add a query variable, click the Query Variables pane and enter a JSON object that defines your variable. To use a variable in your query, prepend the $ character to your variable name and use it to replace the desired value.
Multiple arguments can be used together in the same query. For example, you can use the where argument to filter the results and then use the order_by argument to sort them.
The two answers (from Bens and Nimish) are wrong in this context, they're not specific to Gatsby.
If you want to use variables in the GraphQL queries, you have to pass them via context in the createPage
function in gatsby-node.js
as explained here:
https://www.gatsbyjs.org/docs/programmatically-create-pages-from-data/
You currently can't use variables outside of that, e.g. have a look at this discussion: https://spectrum.chat/?t=abee4d1d-6bc4-4202-afb2-38326d91bd05
So to pass variables you have to use the following syntax
graphql(`<your_query_with_variable>`, { indexPage: <value_of_variable> })
So the query will come something like this
export const query = grapqhl(
`query getImages($fileName: String) {
landscape: file(relativePath: {eq: $fileName}) {
childImageSharp {
fluid(maxWidth: 1000) {
base64
tracedSVG
aspectRatio
src
srcSet
srcWebp
srcSetWebp
sizes
originalImg
originalName
}
}
}
}
`,
{fileName: "knight.jpg"}
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With