简体   繁体   English

graphQL查询中的变量

[英]Variables in graphQL queries

EDIT: now with working code below 编辑:现在使用下面的工作代码

The GraphiQL version GraphiQL版本

I have this query to fetch a gatsby-image : 我有这个查询来获取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 . 以上在GraphiQL中工作正常。

The Gatsby version 盖茨比版

Now I want to use it in Gatsby, so I have the following code: 现在我想在Gatsby中使用它,所以我有以下代码:

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: 编辑:

The working version 工作版

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;

The two answers (from Bens and Nimish) are wrong in this context, they're not specific to Gatsby. 这两个答案(来自Bens和Nimish)在这种情况下是错误的,它们不是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/ 如果你想在GraphQL查询中使用变量,你必须通过gatsby-node.js中的createPage函数中的上下文传递它们,如下所述: httpsgatsby-node.js 从数据/

You currently can't use variables outside of that, eg have a look at this discussion: https://spectrum.chat/?t=abee4d1d-6bc4-4202-afb2-38326d91bd05 您目前无法使用其他变量,例如,请查看此讨论: https//spectrum.chat/?t = abee4d1d-6bc4-4202-afb2-38326d91bd05

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

)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM