【问题标题】:Unknown argument "fields" on field "markdownRemark" of type "Query"“查询”类型的字段“markdownRemark”上的未知参数“字段”
【发布时间】:2020-11-27 23:18:37
【问题描述】:

我在访问markdownRemarkfields 属性时遇到了麻烦,当进入 graghql 游乐场时我找不到fields gatsby-node.js

const path = require(`path`)
const { createFilePath } = require(`gatsby-source-filesystem`);

exports.onCreate = ({ node, getNode, actions }) => {
    const { createNodeField } = actions
    if (node.internal.type === `MarkdownRemark`) {
        const slug = createFilePath({ node, getNode })

        createNodeField({
            node,
            name: `slug`,
            value: slug
        })
    } 
}

exports.createPage = ({ graphql, actions }) => {
    const { createPage } = actions
    return grapgh(`
        allMarkdownRemark {
            edges {
                node {
                    fields {
                        slug
                    }
                }
            }
        }
    `).then(result => {
        result.data.allMarkdownRemark.edges.forEach(({node}) => {          
            createPage({
                path: node.fields.slug,
                component: path.resolve(`./src/templates/blog-post.js`),
                context: node.fields.slug
            })
        })

    })
}

gatsby-config.js

module.exports = {
  siteMetadata: {
    title: `OB blog`,
    description: `Kick off your next, great Gatsby project with this default starter. This barebones starter ships with the main Gatsby configuration files you might need.`,
    author: `@gatsbyjs`,
  },
  plugins: [
    `gatsby-plugin-react-helmet`,
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        name: `images`,
        path: `${__dirname}/src/images`,
      },
      resolve: `gatsby-source-filesystem`,
      options: {
        name: `markdowns`,
        path: `${__dirname}/src/markdown-pages`,
      }
    },
    `gatsby-transformer-remark`,
    `gatsby-transformer-sharp`,
    `gatsby-plugin-sharp`,
    {
      resolve: `gatsby-plugin-manifest`,
      options: {
        name: `gatsby-starter-default`,
        short_name: `starter`,
        start_url: `/`,
        background_color: `#663399`,
        theme_color: `#663399`,
        display: `minimal-ui`,
        icon: `src/images/gatsby-icon.png`, // This path is relative to the root of the site.
      },
    },
    // this (optional) plugin enables Progressive Web App + Offline functionality
    // To learn more, visit: https://gatsby.dev/offline
    // `gatsby-plugin-offline`,
  ],
}

【问题讨论】:

    标签: reactjs graphql gatsby


    【解决方案1】:

    你应该使用:

    const { createFilePath } = require(`gatsby-source-filesystem`)
    
    exports.onCreateNode = ({ node, getNode, actions }) => {
      const { createNodeField } = actions
      if (node.internal.type === `MarkdownRemark`) {
        const slug = createFilePath({ node, getNode, basePath: `pages` })
        createNodeField({
          node,
          name: `slug`,
          value: slug,
        })
      }
    }
    

    请注意,您使用的是 onCreate 而不是 onCreateNode API。有关Gatsby Node APIs 的更多信息。来自文档:

    在创建新节点时调用。希望扩展的插件或 其他插件创建的转换节点应该实现这个 API。 另请参阅 createNodecreateNodeField 的文档。

    【讨论】:

      猜你喜欢
      • 2019-01-15
      • 2021-06-02
      • 2020-09-24
      • 2021-09-05
      • 2020-05-18
      • 2017-02-03
      • 1970-01-01
      • 2019-03-28
      • 1970-01-01
      相关资源
      最近更新 更多