javascript - GatsbyJS - 向 graphQL 添加变量
问题描述
我正在构建一个动态 gatsby 应用程序,用户可以在其中登录并查看类别列表,以及每个类别的产品列表。当用户点击一个类别时,我想通过 graphQL 查询动态呈现相关产品。我想如何做到这一点是通过从 URL 中获取类别 slug 并将其传递给 graphQL 查询,但是当我传递变量时我不断收到错误。
我在下面粘贴了我的查询,没有变量。我想要一个变量 ${slug} 而不是“无服务器”。
export const query = graphql`
{
fauna {
slugMap (slug: "serverless"){
data{
name
companies{
data{
name
website
description
}
}
}
}
}
}
`
Gatsby-node.js
exports.onCreatePage = async ({ page, actions }) => {
const { createPage } = actions
// Only update the `/app` page.
if (page.path.match(/^\/map/)) {
// page.matchPath is a special key that's used for matching pages
// with corresponding routes only on the client.
page.matchPath = "/map/*"
// Update the page.
createPage(page)
}
}
解决方案
Gatsby 查询用于在开发或构建过程中查询内部状态- 在部署后它不活动(不存在)。
对于动态查询,您需要活动/实时数据源- 一些 graphql 服务器。您可以使用 graphql 客户端 (fe apollo) 或fetch
(POST json
graphql requests)。这在 gatsby dosc - data-fetching中有解释
推荐阅读
- javascript - 寻找合适的代码架构来嵌入音频,同时防止页面更新闪烁(HTML/javascript/Ajax)
- css - 网格列表向左浮动
- c# - 通过引用传递 DataGridViewCell
- gcc - 为什么我的内联汇编代码会导致三重错误?
- r - 在 tsCV 中循环预测函数
- python - 在 Python 函数调用中创建生成器 - 为什么会这样?
- html - 如何在 html/EJS 中将图像发送到电子邮件
- android - 将我的 Unity android 升级到 64 位后,在 Google Play 上上传 apk 后仍然出现错误。不符合 64 位
- javascript - 我的 Json 数组只返回最后一个数组。如何解决?
- python - 如何在 Bokeh 中添加多行 x 轴标签?