[英]Gatsby-node error: “Must Provide Source” error being thrown
I'm creating dynamic pages in Gatsby, pulling data from Fauna.我在 Gatsby 中创建动态页面,从 Fauna 中提取数据。 I've got a query in gastby-node that is throwing an error "Must provide Source", but the query works in GraphiQL.我在 gastby-node 中有一个查询抛出错误“必须提供源”,但该查询在 GraphiQL 中有效。 I've include gatsby-node.js below.我在下面包含了 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
},
})
})
}
Today I encountered the same error and after some time figured it out.今天我遇到了同样的错误,过了一段时间才弄清楚。 A stupid mistake tbh.一个愚蠢的错误。
graphql
is a function that needs to be called with the query as the parameter ( graphql(`...`)
). graphql
是一个 function 需要使用查询作为参数( graphql(`...`)
)调用。 I was mistaking it for graphql-tag
which I used in apollo as gql`...`
我把它误认为是我在阿波罗中使用的graphql-tag
为gql`...`
This should work这应该工作
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,
},
})
})
}
Hope this helps !希望这可以帮助 !
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.