【问题标题】:Gatsby Query type not showing in graphiQlGatsby 查询类型未在 graphiQl 中显示
【发布时间】:2019-12-18 06:46:05
【问题描述】:

我正在创建一个 gatsby 插件。在开发时,我没有收到任何错误或警告,但在 graphiql(或http://localhost:8000/___graphql)中看不到我的查询

module.exports = {
  siteMetadata: {
    title: `Gatsby Default Starter`,
    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`,
      },
    },
    `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.
      },
    },
    {
      resolve: 'gatsby-source-custom',
      options: {
        workspaceId: 'spaceyfi-dummy',
        schemaId: 'custom-development'
      }
    },
    // this (optional) plugin enables Progressive Web App + Offline functionality
    // To learn more, visit: https://gatsby.dev/offline
    // `gatsby-plugin-offline`,
  ],
}

这是我在根目录中的插件文件夹结构

文件夹内index.js是空文件

getsby-node.js 看起来像这样

 const selfSdk = require('selfSdk')

 function createNodeContent (data, id, type) {
  const nodeId = createNodeId(id)
  const nodeContent = JSON.stringify(data)
  const contentDigest = createContentDigest(data)
  const nodeMeta = {
    id: nodeId,
    parent: null,
    children: [],
    internal: {
      type: type, // used to generate the resulting GraphQL query name
      content: nodeContent,
      contentDigest
    }
  }
  const node = Object.assign({}, data, nodeMeta)
  return node
}

exports.sourceNodes = async ({ actions, createNodeId, createContentDigest }, {
  workspaceId, 
  schemaId
}) => {
  const { createNode, setPluginStatus } = actions
  const workspace = selfSdk.getWorkspace(workspaceId)
  console.log(workspaceId, schemaId)
  // If there is schemaId but we don't have itemId, load all posts 
  try {
      const itemsList = await workspace.read(schemaId)
      const type = `getPosts`
      itemsList.forEach(({data, id}) => {
        createNode(createNodeContent(data, id, type))
      })


} catch (error) {
    console.error(`Error Fetching data`)
    console.error(error)
  }
  setPluginStatus({
    status: {
      lastFetched: Date.now()
    }
  }) 
}

这是我的 grphiql

【问题讨论】:

  • 尝试调试itemsList的值。 createNode() 也会返回一个 Promise。
  • @Z.Zlatev 无法理解您的评论抱歉。
  • 你是通过 babel 或类似的方式运行src 吗?否则,gatsby-node.js 必须在根目录中,即在 index.js IIRC 的旁边

标签: javascript gatsby


【解决方案1】:

Gatsby 将在插件文件夹的根目录中查找 gatsby-node.js

plugins/
– your-plugin/
-- gatsby-node.js
-- package.json

https://www.gatsbyjs.org/docs/files-gatsby-looks-for-in-a-plugin/

https://www.gatsbyjs.org/docs/creating-a-source-plugin/#what-does-the-code-look-like

这就是它没有出现在您的 GraphiQL 中的原因。您的插件不会生成任何节点,因为您有一个空白的 index.js,所以它看起来像是一个不导出任何内容的模块。

【讨论】:

    猜你喜欢
    • 2020-07-23
    • 2018-06-15
    • 2020-09-27
    • 1970-01-01
    • 2020-06-28
    • 2021-06-20
    • 2019-10-26
    • 2020-10-22
    • 2019-10-04
    相关资源
    最近更新 更多