Gatsby 源插件只显示 GraphQL 数组中的最后一项
Gatsby source plugin only showing last item in array in GraphQL
当我在 运行 构建 6 个对象后进行控制台登录时,我的 drinks 数组中出现了这些对象。当我 运行 发育时,它们也会出现。但是当我查询 graphQL 时,只有数组中的最后一个对象可用。我是 gatsby 和 graphQL 的新手,所以包含图像以防我的查询关闭。
代码来自我的 gatsby-node.js 文件:
exports.sourceNodes = async (
{ actions, createContentDigest, createNodeId,}
) => {
const NODE_TYPE = "CocktailRecipes";
try{
const response = await fetch(`https://www.thecocktaildb.com/api/json/v1/1/search.php?s=vodka`)
const data = await response.json();
const {drinks} = data;
console.log(drinks)
drinks.forEach((drink) => {
actions.createNode({
...drink,
id: createNodeId(`${NODE_TYPE }-${drink.id}`),
parent:null,
children:[],
internal:{
type:NODE_TYPE,
content:JSON.stringify(drink),
contentDigest: createContentDigest(drink)
}
})
})
}
catch(error){
console.log("ERROR")
console.log(error)
}
}
only one object showing in graphQL
如果有人能提供帮助,我将不胜感激,因为我已经用头撞墙了一段时间了。我已经完成了盖茨比清洁。我试过 map 而不是 forEach
几个月前我遇到了与您完全相同的问题,在我的情况下,我需要为每个元素设置一个有效的内部 id
以允许 GraphQL 创建一个合适的模式如果不是每个节点,则 id
在每个元素中被覆盖,它只取最后一个。
在您的情况下,似乎某些字段有误,导致以下表达式无效:
id: createNodeId(`${NODE_TYPE }-${drink.id}`),
尝试调试更多正在接收的内容并将其更改为一些硬编码值。类似于:
id: drink.id,
请记住,如果每个节点的 id
不同,您不需要使用 createNodeId
API 进行调试(但建议这样做)。
当我在 运行 构建 6 个对象后进行控制台登录时,我的 drinks 数组中出现了这些对象。当我 运行 发育时,它们也会出现。但是当我查询 graphQL 时,只有数组中的最后一个对象可用。我是 gatsby 和 graphQL 的新手,所以包含图像以防我的查询关闭。 代码来自我的 gatsby-node.js 文件:
exports.sourceNodes = async (
{ actions, createContentDigest, createNodeId,}
) => {
const NODE_TYPE = "CocktailRecipes";
try{
const response = await fetch(`https://www.thecocktaildb.com/api/json/v1/1/search.php?s=vodka`)
const data = await response.json();
const {drinks} = data;
console.log(drinks)
drinks.forEach((drink) => {
actions.createNode({
...drink,
id: createNodeId(`${NODE_TYPE }-${drink.id}`),
parent:null,
children:[],
internal:{
type:NODE_TYPE,
content:JSON.stringify(drink),
contentDigest: createContentDigest(drink)
}
})
})
}
catch(error){
console.log("ERROR")
console.log(error)
}
}
only one object showing in graphQL
如果有人能提供帮助,我将不胜感激,因为我已经用头撞墙了一段时间了。我已经完成了盖茨比清洁。我试过 map 而不是 forEach
几个月前我遇到了与您完全相同的问题,在我的情况下,我需要为每个元素设置一个有效的内部 id
以允许 GraphQL 创建一个合适的模式如果不是每个节点,则 id
在每个元素中被覆盖,它只取最后一个。
在您的情况下,似乎某些字段有误,导致以下表达式无效:
id: createNodeId(`${NODE_TYPE }-${drink.id}`),
尝试调试更多正在接收的内容并将其更改为一些硬编码值。类似于:
id: drink.id,
请记住,如果每个节点的 id
不同,您不需要使用 createNodeId
API 进行调试(但建议这样做)。