【问题标题】:Gatsby-node error: "Must Provide Source" error being thrownGatsby 节点错误:抛出“必须提供源”错误
【发布时间】:2020-06-09 01:23:15
【问题描述】:

我正在 Gatsby 中创建动态页面,从 Fauna 中提取数据。我在 gastby-node 中有一个查询抛出错误“必须提供源”,但该查询在 GraphiQL 中有效。我在下面包含了 gatsby-node.js。

exports.createPages = async function({actions, graphql}){
    const {data} = await graphql`
    query {
        fauna {
            allCompanies {
                data {
                    slug
                }
            }
        }
    }
    `

data.fauna.allCompanies.data.forEach(edge => {
    const slug = edge.slug
    actions.createPages({
        path: slug,
        component: require.resolve("./src/components/products.js"),
        context:{
            slug
        },
    })
})
}

【问题讨论】:

  • 我们能看到错误信息吗?你确定它来自 GraphQL 查询吗?

标签: javascript reactjs gatsby


【解决方案1】:

今天我遇到了同样的错误,过了一段时间才弄明白。一个愚蠢的错误。

graphql 是一个需要以查询为参数调用的函数(graphql(`...`))。我把它误认为是我在阿波罗中使用的graphql-tag gql`...`

这应该可以工作

exports.createPages = async function ({ actions, graphql }) {
  /* 
     you have to pass the template literal query
     in the function graphql(`...`)

  */
  const { data } = await graphql(`
    query {
      fauna {
        allCompanies {
          data {
            slug
          }
        }
      }
    }
  `)

  data.fauna.allCompanies.data.forEach(edge => {
    const slug = edge.slug
    actions.createPages({
      path: slug,
      component: require.resolve("./src/components/products.js"),
      context: {
        slug,
      },
    })
  })
}

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-22
    • 2018-12-01
    • 2011-08-20
    • 2020-11-05
    • 2018-04-01
    • 2019-01-11
    • 2016-09-18
    • 2022-12-10
    相关资源
    最近更新 更多