简体   繁体   English

graphQL 查询中的字符串插值

[英]String interpolation in graphQL query

I am new to Gatsby and its graphQL query system to retrieve assets.我是 Gatsby 及其 graphQL 查询系统来检索资产的新手。 I have a working component Image that fetches an image and displays it.我有一个工作组件Image可以获取图像并显示它。 I want to have the name of the image customizable but I can't figure out how to dit.我想自定义图像的名称,但我不知道如何编辑。

Here is the working component:这是工作组件:

const Image = () => (
  <StaticQuery
    query={graphql`
      query {
        // fetching the image gatsby-astronaut.png
        placeholderImage: file(relativePath: { eq: "gatsby-astronaut.png" }) {
          childImageSharp {
            fluid(maxWidth: 300) {
              ...GatsbyImageSharpFluid
            }
          }
        }
      }
    `}
    render={data => <Img fluid={data.placeholderImage.childImageSharp.fluid} />}
  />
);

And here is what I tried to have a customizable image:这是我试图拥有一个可定制的图像:

const Image = ({ imgName }: { imgName: string }) => (
  <StaticQuery
    query={graphql`
      query {
        // fetching the image imgName
        placeholderImage: file(relativePath: { eq: "${imgName}.png" }) {
          childImageSharp {
            fluid(maxWidth: 300) {
              ...GatsbyImageSharpFluid
            }
          }
        }
      }
    `}
    render={data => <Img fluid={data.placeholderImage.childImageSharp.fluid} />}
  />
);

But it raises the following error for the query:但它会为查询引发以下错误:

Expected 1 arguments, but got 2.ts(2554)

How can I have a customizable image name?我怎样才能有一个可自定义的图像名称?

Check the docs for static query检查文档以获取静态查询

StaticQuery can do most of the things that page query can, including fragments. StaticQuery 可以做页面查询可以做的大部分事情,包括片段。 The main differences are:主要区别是:

  • page queries can accept variables (via pageContext) but can only be added to page components页面查询可以接受变量(通过 pageContext)但只能添加到页面组件
  • StaticQuery does not accept variables (hence the name “static”), but can be used in any component, including pages StaticQuery 不接受变量(因此称为“静态”),但可以在任何组件中使用,包括页面

So you might want to query for the image's GatsbyImageSharpFluid in your page query and pass it as the fluid prop directly to gatsby image.因此,您可能希望在页面查询中查询图像的GatsbyImageSharpFluid ,并将其作为流体属性直接传递给 gatsby 图像。

Here is the easy way I ran into:这是我遇到的简单方法:

const Image = props => {
  const data = useStaticQuery(graphql`
    query {
      firstImg: file(relativePath: { eq: "firstImg.png" }) {
        childImageSharp {
          fluid(maxWidth: 300) {
            ...GatsbyImageSharpFluid
          }
        }
      }

      secondImg: file(
        relativePath: { eq: "secondImg.png" }
      ) {
        childImageSharp {
          fluid(maxWidth: 300) {
            ...GatsbyImageSharpFluid
          }
        }
      }
    }
  `)

  switch (props.name) {
    case "firstImg":
      return <Img fluid={data.firstImg.childImageSharp.fluid} />
    case "secondImg":
      return <Img fluid={data.secondImg.childImageSharp.fluid} />
    default:
      return <Img />
  }
}

and use it like this:并像这样使用它:

<Image name="firstImg" />

You can also make it typo-safe by introducing an object with all the images you might want to display, like this:您还可以通过引入一个包含您可能想要显示的所有图像的对象来使其错字安全,如下所示:

const Images = { firstImg: 'firstImg', secondImg: 'secondImg' }

and then use it like this:然后像这样使用它:

<Image name={Images.firstImage} />

and

...
switch (props.name) {
case Images.firstImage:
...

Using ksav answer I managed to make this work by using pageQuery and receive the images i want to use in a specific page through props .使用ksav回答我设法用pageQuery,使这项工作并接收我想通过在特定页面使用的图像道具

Like this:像这样:

export const pageQuery = graphql`
  coverImage: file(relativePath: { eq: "coverImage.png" }) {
    childImageSharp {
      fluid(maxWidth: 600) {
        ...GatsbyImageSharpFluid_tracedSVG
      }
    }
  }
}`

If you add this to your page component you will receive the props coverImage with the coverImage.png image.如果您将此添加到您的页面组件,您将收到带有 coverImage.png 图像的道具 coverImage。 Hope this helps!希望这可以帮助!

This is one area where Gatsby could do better imho.这是盖茨比可以做得更好的一个领域。 I appreciate @ramzesenok's answer and if you must use the Gatsby framework with graphql that's probably the best you're going to do.我很欣赏@ramzesenok 的回答,如果您必须将 Gatsby 框架与 graphql 一起使用,那可能是您最好的选择。 But you could also just fall back to normal React like this:但是你也可以像这样回到正常的 React

import React from 'react';
import gatsbyAstronaut from './gatsby-astronaut.png'; 

and use it like this.并像这样使用它。

<img src={gatsbyAstronaut} alt="Gatsby Astronaut" />

Even this seems a little heavy but the import is required so webpack can bundle the image properly.即使这看起来有点重,但需要导入,因此 webpack 可以正确捆绑图像。

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

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