由于我的GraphQL查询中的错误,构建失败

时间:2020-05-05 09:42:16

标签: collections graphql gatsby netlify-cms

“类别”字段未通过MarkdownRemarkFrontmatterFilterInput类型定义。这是什么意思,请告知。

我将Netlify CMS用作带有Gatsbyjs前端的Content Manager。我正在使用此入门模板https://github.com/netlify-templates/gatsby-starter-netlify-cms,它们以类似的方式显示标签,我想显示类别,因此我复制了与标签相关的文件,并将标签替换为类别集合,还在config.yml文件中添加了类别集合。下面是该代码。 生成错误 https://prnt.sc/sb6pwd该错误在index.js文件中。

category.js 这是我的模板文件,用于列出所有类别以及该类别的博客总数

import React from 'react'
import { Helmet } from 'react-helmet'
import { Link, graphql } from 'gatsby'
import Layout from '../components/Layout'

class CategoryRoute extends React.Component {
  render() {
    const posts = this.props.data.allMarkdownRemark.edges
    const postLinks = posts.map((post) => (
      <li key={post.node.fields.slug}>
        <Link to={post.node.fields.slug}>
          <h2 className="is-size-2">{post.node.frontmatter.title}</h2>
        </Link>
      </li>
    ))
    const category = this.props.pageContext.category
    const title = this.props.data.site.siteMetadata.title
    const totalCount = this.props.data.allMarkdownRemark.totalCount
    const categoryHeader = `${totalCount} post${
      totalCount === 1 ? '' : 's'
    } tagged with “${category}”`

    return (
      <Layout>
        <section className="section">
          <Helmet title={`${category} | ${title}`} />
          <div className="container content">
            <div className="columns">
              <div
                className="column is-10 is-offset-1"
                style={{ marginBottom: '6rem' }}
              >
                <h3 className="title is-size-4 is-bold-light">{categoryHeader}</h3>
                <ul className="taglist">{postLinks}</ul>
                <p>
                  <Link to="/category/">Browse all category</Link>
                </p>
              </div>
            </div>
          </div>
        </section>
      </Layout>
    )
  }
}

export default CategoryRoute

export const categoryPageQuery = graphql`
  query CategoryPage($category: String) {
    site {
      siteMetadata {
        title
      }
    }
    allMarkdownRemark(
      limit: 1000
      sort: { fields: [frontmatter___date], order: DESC }
      filter: { frontmatter: { category: { in: [$category] } } }
    ) {
      totalCount
      edges {
        node {
          fields {
            slug
          }
          frontmatter {
            title

          }
        }
      }
    }
  }
`

index.js

import React from 'react'
import { kebabCase } from 'lodash'
import { Helmet } from 'react-helmet'
import { Link, graphql } from 'gatsby'
import Layout from '../../components/Layout'

const CategoryPage = ({
  data: {
    allMarkdownRemark: { group },
    site: {
      siteMetadata: { title },
    },
  },
}) => (
  <Layout>
    <section className="section">
      <Helmet title={`Category | ${title}`} />
      <div className="container content">
        <div className="columns">
          <div
            className="column is-10 is-offset-1"
            style={{ marginBottom: '6rem' }}
          >
            <h1 className="title is-size-2 is-bold-light">Categories</h1>
            <ul className="taglist">
              {group.map((category) => (
                <li key={category.fieldValue}>
                  <Link to={`/category/${kebabCase(category.fieldValue)}/`}>
                    {category.fieldValue} ({category.totalCount})
                  </Link>
                </li>
              ))}
            </ul>
          </div>
        </div>
      </div>
    </section>
  </Layout>
)

export default CategoryPage

export const categoryPageQuery = graphql`
  query CategoryQuery {
    site {
      siteMetadata {
        title
      }
    }
    allMarkdownRemark(limit: 1000) {
      group(field: frontmatter___category) {
        fieldValue
        totalCount
      }
    }
  }
`

Config.yml 类别集合声明:

- name: category
    label: Category
    identifier_field: slug
    folder: src/pages/blog/category
    create: true
    fields:
      -label: Template Key
       name: templateKey
       widget: hidden
       default: category
      - label: Category
        name: category
        widget: string
      - label: Category Image
        name: category_img
        widget: image
      - label: Slug
        name: slug
        widget: string
  - name: blog
    label: Blog
    folder: src/pages/blog
    create: true
    slug: "{{year}}-{{month}}-{{day}}-{{slug}}"
    fields:
      - label: Template Key
        name: templateKey
        widget: hidden
        default: blog-post
      - label: Title
        name: title
        widget: string
      - label: Path
        name: path
        widget: hidden
        default: blog-post
      - label: Publish Date
        name: date
        widget: datetime
      - label: Description
        name: description
        widget: text
      - label: Featured Post
        name: featuredpost
        widget: boolean
      - label: Featured Image
        name: featuredimage
        widget: image
      - label: Body
        name: body
        widget: markdown
      - label: Tags
        name: tags
        widget: list
      - label: Category
        name: category
        widget: select
        options:
          - label: Iraq
            value: iraq
          - label: Personal
            value: personal
          - label: Productivity
            value: productivity
          - label: Software Development
            value: software-development
          - label: Uncategorized
            value: uncategorized

1 个答案:

答案 0 :(得分:0)

您需要为此字段添加一些初始数据。如果您查看控制台,可能会看到以下内容:

  • 您想有选择地使用您的字段,而现在没有在任何地方使用它。因此,盖茨比无法推断类型并将其添加到 GraphQL模式。一种快速的解决方法是在其中添加至少一个条目 字段(“虚拟内容”)