Variables in graphQL queries

后端 未结 2 462
遇见更好的自我
遇见更好的自我 2020-12-11 02:50

EDIT: now with working code below

The GraphiQL version

I have this query to fetch a gatsby-image:

query getImages($fileName: String) {
  la         


        
相关标签:
2条回答
  • 2020-12-11 03:15

    So to pass variables you have to use following syntax

    graphql(`<your_query_with_variable>`, { indexPage: <value_of_variable> })
    

    So 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"}
    

    )

    0 讨论(0)
  • 2020-12-11 03:23

    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

    0 讨论(0)
提交回复
热议问题