首页 > 解决方案 > gatsby graqpql query rest API 只返回一个结果

问题描述

我已经开始使用 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;
};

标签: restgraphqlgatsby

解决方案


我认为一个简单的错字导致了问题。

改变:

return createNode({ id: `£{index}`, 

至:

return createNode({ id: `${index}`, 

并且在空腿节点 id: 内相同${index}

这应该可以修复错误。


推荐阅读