如何查询2张不同的图片?

问题描述

我想查询2张图片images/me.jpgimages/Boxing.png

不确定在graphql中怎么做:

query AboutQuery {
    file(relativePath: { eq: ("images/me.jpg"|"images.Boxing.png") }) {
      childImageSharp {
        fluid {
          ...GatsbyImageSharpFluid
        }
      }
    }
  }

解决方法

这应该有效:

query AboutQuery {
    file(relativePath: { regex: "images" }) {
      childImageSharp {
        fluid {
          ...GatsbyImageSharpFluid
        }
      }
    }
  }

GraphQL Query Reference中可以看到,可以使用正则表达式来匹配relativePath,如果需要,您可以匹配"images"

您也可以选择使用in过滤器(接受数组中的多个值)来匹配["images"]

query AboutQuery {
    file(relativePath: { in: ["images"] }) {
      childImageSharp {
        fluid {
          ...GatsbyImageSharpFluid
        }
      }
    }
  }

作为单个值,正则表达式是最好的方法。