[英]Gatsby-node error: “Must Provide Source” error being thrown
我在 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
是一个 function 需要使用查询作为参数( 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,
},
})
})
}
希望这可以帮助 !
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.