5

将 netlify cms 添加到站点时,如何让 slug 出现在 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 查询: 在此处输入图像描述

4

1 回答 1

8

GraphQLslug查询中的不是config.yml. 这些 slug 字段不相关。您在查询中引用的那个来自 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,
    })
  }
}
于 2018-09-07T16:11:41.237 回答