如何使用NetlifyCMS和Gatsby将子弹显示在graphql中?

时间:2018-09-06 23:22:28

标签: gatsby netlify netlify-cms

在站点上添加netlify cms时,如何将子弹显示在graphql中? 我有一个博客文章集,除了子弹之外,其他所有内容都显示出来:

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查询: enter image description here

1 个答案:

答案 0 :(得分:2)

GraphQL查询中的slug并不是config.yml中字段最重要的部分。这些段字段无关。您在查询中指的是Gatsby中的节点。

您的fields中缺少上述GraphQL查询中的node值。如下例所示,必须通过添加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,
    })
  }
}