graphQL 查询中的变量

IT技术 javascript reactjs graphql gatsby
2021-04-14 14:10:13

编辑:现在有下面的工作代码

GraphiQL 版本

我有这个查询来获取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
      }
    }
  }
}

然后这个查询变量:

{
  "fileName": "titanic.jpg"
}

以上在GraphiQL 中工作正常

盖茨比版本

现在我想在 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"}
)

以上是行不通的。 data.landscape.childImageSharp === null

我究竟做错了什么?

编辑:

工作版本

谢谢您的帮助!下面的代码工作得很好。这篇文章特别有帮助。这不是一个理想的解决方案,但它对我有用。

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;
2个回答

在这种情况下,这两个答案(来自 Bens 和 Nimish)是错误的,它们并不是 Gatsby 特有的。

如果要在 GraphQL 查询中使用变量,则必须通过createPage函数中的上下文传递它们gatsby-node.js如下所述:https : //www.gatsbyjs.org/docs/programmatically-create-pages-from-data/

您目前不能使用除此之外的变量,例如查看此讨论:https : //spectrum.chat/?t=abee4d1d-6bc4-4202-afb2-38326d91bd05

可悲的是,情况就是这样。我已经根据第二个链接添加了一些工作代码。
2021-06-08 14:10:13

因此,要传递变量,您必须使用以下语法

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

所以查询会是这样的

    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"}
   )
注意:这仅适用于gatsby-node文件,使用静态查询(例如)将变量传递给 graphql 方法将不允许使用变量。
2021-06-12 14:10:13