如何使用 NetlifyCMS 和 Gatsby 让 slug 出现在 graphql 中?
How to get the slug to show up in graphql using NetlifyCMS and Gatsby?
将 netlify cms 添加到站点时,如何让 slug 出现在 graphql 中?
我有一个博客集 post,除了 slug:
之外的所有内容都显示了
backend:
name: git-gateway
branch: master # Branch to update (optional; defaults to master)
media_folder: static/images
public_folder: /images
collections:
- name: "blog"
label: "Blog"
folder: "content/blogPost"
create: true
slug: "{{year}}-{{month}}-{{day}}-{{slug}}"
fields: # The fields for each document, usually in front matter
- { label: "Layout", name: "layout", widget: "hidden", default: "blog" }
- { label: "Title", name: "title", widget: "string" }
- { label: "Publish Date", name: "date", widget: "datetime" }
- { label: "Featured Image", name: "thumbnail", widget: "image" }
- { label: "Body", name: "body", widget: "markdown" }
这是我的 graphql 查询:
GraphQL 查询中的 slug
是 而不是 config.yml
中字段的 frontmatter 的一部分。这些 slug 字段不相关。您在查询中提到的那个来自 Gatsby 中的节点。
您的 node
中缺少上述 GraphQL 查询中的 fields
值。这必须使用 gatsby-node.js
配置通过添加它来传递,如本例所示:
const { createFilePath } = require('gatsby-source-filesystem')
exports.onCreateNode = ({ node, boundActionCreators, getNode }) => {
const { createNodeField } = boundActionCreators
if (node.internal.type === `MarkdownRemark`) {
const value = createFilePath({ node, getNode })
createNodeField({
name: `slug`,
node,
value,
})
}
}
将 netlify cms 添加到站点时,如何让 slug 出现在 graphql 中? 我有一个博客集 post,除了 slug:
之外的所有内容都显示了backend:
name: git-gateway
branch: master # Branch to update (optional; defaults to master)
media_folder: static/images
public_folder: /images
collections:
- name: "blog"
label: "Blog"
folder: "content/blogPost"
create: true
slug: "{{year}}-{{month}}-{{day}}-{{slug}}"
fields: # The fields for each document, usually in front matter
- { label: "Layout", name: "layout", widget: "hidden", default: "blog" }
- { label: "Title", name: "title", widget: "string" }
- { label: "Publish Date", name: "date", widget: "datetime" }
- { label: "Featured Image", name: "thumbnail", widget: "image" }
- { label: "Body", name: "body", widget: "markdown" }
这是我的 graphql 查询:
GraphQL 查询中的 slug
是 而不是 config.yml
中字段的 frontmatter 的一部分。这些 slug 字段不相关。您在查询中提到的那个来自 Gatsby 中的节点。
您的 node
中缺少上述 GraphQL 查询中的 fields
值。这必须使用 gatsby-node.js
配置通过添加它来传递,如本例所示:
const { createFilePath } = require('gatsby-source-filesystem')
exports.onCreateNode = ({ node, boundActionCreators, getNode }) => {
const { createNodeField } = boundActionCreators
if (node.internal.type === `MarkdownRemark`) {
const value = createFilePath({ node, getNode })
createNodeField({
name: `slug`,
node,
value,
})
}
}