gatsby-graqpql查询restapi只返回一个结果
我已经开始使用gatsby和graphQL,并试图从RESTAPI获取dat。我能够循环浏览数据并将其记录到控制台中。然而,当使用graphQL时,我只得到一个结果。请看下面我的代码。我感谢任何对我所做错事的帮助gatsby-graqpql查询restapi只返回一个结果,rest,graphql,gatsby,Rest,Graphql,Gatsby,我已经开始使用gatsby和graphQL,并试图从RESTAPI获取dat。我能够循环浏览数据并将其记录到控制台中。然而,当使用graphQL时,我只得到一个结果。请看下面我的代码。我感谢任何对我所做错事的帮助 exports.sourceNodes = async ({ boundActionCreators }) => { const { createNode } = boundActionCreators; console.log(createNode) const fet
exports.sourceNodes = async ({ boundActionCreators }) => {
const { createNode } = boundActionCreators;
console.log(createNode)
const fetchData = () => axios.get(url)
const res = await fetchData();
console.log(res.data);
res.data.blogArticle.forEach((article, index) => {
return createNode({
...article,
id: `£{index}`,
children: [],
parent: `__SOURCE__`,
internal: {
type: `BlogArticle`,
content: JSON.stringify(article),
contentDigest: crypto
.createHash(`md5`)
.update(JSON.stringify(article))
.digest(`hex`)
},
heading: article.heading,
subheading: article.subheading,
content: {
text: article.content.text,
videoURL: article.content.videoURL,
imageURL: article.content.imageURL
}
});
});
res.data.emptyLegs.forEach((leg, index) => {
const emptyLegNode = {
from: leg.From,
to: leg.To,
aircraft: leg.Aircraft,
seats: leg.Seats,
date: leg.Date,
price: leg.Price,
id: `£{index}`,
children: [],
parent: `__SOURCE__`,
internal: {
type: `EmptyLegs`,
contentDigest: crypto
.createHash(`md5`)
.update(JSON.stringify(res.data.emptyLegs))
.digest(`hex`)
},
};
createNode(emptyLegNode);
});
return;
};
我认为是一个简单的打字错误造成了这个问题 更改:
return createNode({ id: `£{index}`,
致:
在空分支节点id中也是如此:${index}
这将修复错误。您能给出graphQL查询的代码吗?当您命名
internal.type
BlogArticle时,您应该能够查询allBlogArticle
。。。您可以检查graphiQlquery{allBlogArticle{edges{node{id heading content{text}}}}}
中的第二部分,我总是只返回一个节点,但是当我控制台articleNode时,它会返回所有节点
return createNode({ id: `${index}`,