我正在Gatsby中创建动态页面,从Fauna中提取数据。我在gastby-node中的查询出现了 "必须提供源 "的错误,但在GraphiQL中的查询是有效的。我在下面附上了gastby-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
},
})
})
}
今天我也遇到了同样的错误,过了一段时间才发现。一个愚蠢的错误tbh。
graphql
是一个需要以查询为参数调用的函数( graphql(`...`)
). 我把它误认为是 graphql-tag
我在apollo中用它作为 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,
},
})
})
}
希望能帮到你!