简体   繁体   English

Gatsby 组件中的多个 graphql 查询

[英]Multiple graphql queries in Gatsby component

I need to run multiple graphQL queries within a component and within the gatsby-node.js file.我需要在一个组件和gatsby-node.js文件中运行多个 graphQL 查询。 (Because Prismic is limited to 20 entries per answer...) (因为 Prismic 每个答案限制为 20 个条目......)

I tried the following, just to see if I could create the graphql loop in the default function:我尝试了以下方法,只是想看看我是否可以在默认的 function 中创建 graphql 循环:

export default () => {
  async function allPosts() {
    let data

    await graphql(`
      query allDitherImages {
        prismic {
          allProjects(sortBy: meta_firstPublicationDate_DESC) {
            totalCount
            pageInfo {
              startCursor
              endCursor
              hasNextPage
              hasPreviousPage
            }
            edges {
              node {
                cover_image
                cover_imageSharp {
                  name
                }
              }
            }
          }
        }
      }
    `).then(initialRes => {
      data = initialRes
    })

    return data
  }

  allPosts().then(result => {
    console.log(result)
  })
  return null
}

But then Gatsby tells me that Gatsby related 'graphql' calls are supposed to only be evaluated at compile time, and then compiled away. Unfortunately, something went wrong and the query was left in the compiled code.但是后来 Gatsby 告诉我Gatsby related 'graphql' calls are supposed to only be evaluated at compile time, and then compiled away. Unfortunately, something went wrong and the query was left in the compiled code. Gatsby related 'graphql' calls are supposed to only be evaluated at compile time, and then compiled away. Unfortunately, something went wrong and the query was left in the compiled code.

How can I run multiple graphql queries?如何运行多个 graphql 查询?

Thank you in advance:) Michael提前谢谢你:)迈克尔

The gatsby-source-prismic-graphql package will create pages for all of your Prismic items (more than just the first 20), as it iterates over all items under the hood, so I'd advise looking into using that if you are looking to generate pages for all of those items. gatsby-source-prismic-graphql package 将为您的所有 Prismic 项目(不仅仅是前 20 个)创建页面,因为它会遍历引擎盖下的所有项目,所以如果您正在寻找,我建议您考虑使用它为所有这些项目生成页面。

But if you need to get all items and pass them in the pageContext or something, you'll need to do the recursion yourself in the gatsby-node.但是,如果您需要获取所有项目并将它们传递到 pageContext 或其他内容中,则需要自己在 gatsby-node 中进行递归。

In the gatsby-node, after you have defined the query, you can use something like this to iterate over the results and push to an array.在 gatsby-node 中,定义查询后,您可以使用类似这样的方法来迭代结果并推送到数组。

let documents = [];
async function getAllDocumentsRecursively (query, prop, endCursor = '') {
  const results = await graphql(query, { after: endCursor })
  const hasNextPage = results.data.prismic[prop].pageInfo.hasNextPage
  endCursor = results.data.prismic[prop].pageInfo.endCursor

  results.data.prismic[prop].edges.forEach(({node}) => {
    documents.push(node)
  });

  if (hasNextPage) {
    await getAllDocumentsRecursively(query, 'allDitherImages ', endCursor)
  }
}
await getAllDocumentsRecursively(documentsQuery, 'allDitherImages ');

Then in your createPage, pass the array into the context:然后在您的 createPage 中,将数组传递到上下文中:

createPage({
    path: `/`+ node._meta.uid,
    component: allDitherTempate,
    context: {
      documents: documents
    }
  })

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

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