【发布时间】:2023-03-19 14:10:01
【问题描述】:
我已经开始使用 gatsby 和 graphQL,并且正在尝试从 rest API 获取 dat。我能够遍历数据并将其登录到控制台。但是,当使用 graphQL 时,我只得到一个结果。请在下面查看我的代码。感谢您对我做错的任何帮助。
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;
};
【问题讨论】:
-
你能给出你的graphQL查询的代码吗?如您命名
internal.typeBlogArticle,您应该可以查询allBlogArticle...您可以查看this answer的第二部分 -
这就是我在 graphiQl
query { allBlogArticle { edges { node { id heading content { text } } } } }中尝试的,我总是只得到一个节点,但是当我控制台 articleNode 它给了我所有这些