如何使用NetlifyCMS和Gatsby让slug显示在graphql中?
将netlify cms添加到站点时,如何让slug显示在graphql中? 我收集了一篇博客文章,除了slug外,所有的东西都显示出来了:如何使用NetlifyCMS和Gatsby让slug显示在graphql中?,gatsby,netlify,netlify-cms,Gatsby,Netlify,Netlify Cms,将netlify cms添加到站点时,如何让slug显示在graphql中? 我收集了一篇博客文章,除了slug外,所有的东西都显示出来了: backend: name: git-gateway branch: master # Branch to update (optional; defaults to master) media_folder: static/images public_folder: /images collections: - name: "blog"
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
中字段的前端部分。这些slug字段不相关。您在查询中引用的是Gatsby中的节点
您的节点
中缺少上面GraphQL查询中的字段
值。必须使用gatsby node.js
config将其添加,如下例所示:
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,
})
}
}
这对我也有帮助。非常感谢。对于像我这样读过这个答案但不理解它的人来说,将它与来自的完整
gastby node.js
文件进行比较会有所帮助。