简体   繁体   English

如何使用 graphql gatsby 中第一个查询的变量从第二个查询获取数据?

[英]How to get data from the second query using a variable from the first query in graphql gatsby?

I'm trying to render all photos from a directory that will be defined in the frontmatter of an mdx file called carouselPhotosDir.我正在尝试渲染目录中的所有照片,该目录将在名为 carouselPhotosDir 的 mdx 文件的 frontmatter 中定义。 I thought that to do this, I would need to query the specific mdx's carouselPhotosDir frontmatter field.我认为要做到这一点,我需要查询特定的 mdx 的 carouselPhotosDir frontmatter 字段。 Then, store that into a variable and then query allFile where the relative path is equal to the carouselPhotosDir frontmatter field that I got from the first query.然后,将其存储到一个变量中,然后查询 allFile,其中相对路径等于我从第一个查询中获得的 carouselPhotosDir frontmatter 字段。 I'm not so sure how to do this.我不太确定该怎么做。 I tried doing the query that you see below but it just queried everything.我尝试执行您在下面看到的查询,但它只是查询了所有内容。 I tested the query with a hardcoded path that is defined in the frontmatter field and it worked so the variable from the first query isnt working.我使用在 frontmatter 字段中定义的硬编码路径测试了查询,它起作用了,所以来自第一个查询的变量不起作用。 Is the variable not defined?变量没有定义? I thought it would work similarily to the id variable.我认为它的工作方式类似于 id 变量。

export const pageQuery = graphql`
  query ProjectQuery($id: String, $carouselPhotosDir: String) {
    mdx(id: { eq: $id }) {
      id
      body
      slug
      frontmatter {
        title
        repo
        carouselPhotosDir
      }
    }
    allFile(filter: { relativeDirectory: { eq: $carouselPhotosDir } }) {
      edges {
        node {
          id
          name
          base
          childImageSharp {
            gatsbyImageData(
              placeholder: BLURRED
              layout: CONSTRAINED
              transformOptions: { cropFocus: CENTER, fit: COVER }
            )
          }
        }
      }
    }
  }
`;

Example mdx frontmatter:

---
title: blob
author: JuanCarlos
thumbnail: ./blob-thumbnail.png
repo: https://github.com/blob/repo
carouselPhotosDir: blob/carousel-photos
order: 3
---

Short answer: you can't.简短的回答:你不能。

Is the variable not defined?变量没有定义?

Indeed.的确。

In GraphQL, fields at each "level" of the request are executed and resolved in parallel.在 GraphQL 中,请求的每个“级别”的字段都是并行执行和解析的。 In your example, mdx and allFile are both fields of the same type (the root query type) so they will be resolved at the same time.在您的示例中, mdxallFile都是同一类型(根查询类型)的字段,因此它们将同时解析。 That means each query essentially is not aware of the other or what the other resolved to.这意味着每个查询基本上都不知道对方或对方解决了什么问题。

If you were using some GraphQL client as Apollo you can use some kind of composing to mix the queries on the client-side but not in this scenario where the queries are run in the build-time.如果您使用某个 GraphQL 客户端作为 Apollo,您可以使用某种组合在客户端混合查询,但在这种查询在构建时运行的场景中则不行。

That said, you have two options:也就是说,您有两个选择:

  • When you createPage in for each template page in the gatsby-node you know which mdx is being created at that time hence you can pass the carouselPhotosDir variable as a context (see https://www.gatsbyjs.com/docs/how-to/querying-data/page-query/ ).当您为createPage gatsby-node中的每个模板页面创建页面时,您知道当时正在创建哪个mdx ,因此您可以将carouselPhotosDir变量作为上下文传递(请参阅https://www.gatsbyjs.com/docs/how-to /查询数据/页面查询/ )。 In that way, carouselPhotosDir will be queried in the gatsby-node but used in the page/template query.这样, carouselPhotosDir将在gatsby-node中查询,但在页面/模板查询中使用。

  • Use JavaScript filtering in the template once the data fetch is done.数据获取完成后,在模板中使用 JavaScript 过滤。 You just need to filter among allFile data the specific carouselPhotosDir for each template.您只需要在所有文件数据中carouselPhotosDir allFile You can use the same pageContext variable as before but not in a GraphQL query but in a JavaScript filtering loop to get the specific node of files.您可以使用与以前相同的 pageContext 变量,但不是在 GraphQL 查询中,而是在 JavaScript 过滤循环中,以获取文件的特定节点。

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

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