【问题标题】:gatsby graqpql query rest API returns only one resultgatsby graqpql query rest API 只返回一个结果
【发布时间】: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 它给了我所有这些

标签: rest graphql gatsby


【解决方案1】:

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

变化:

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

到:

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

和空腿内相同的节点id:${index},

这应该可以修复错误。

【讨论】:

    猜你喜欢
    • 2011-12-18
    • 1970-01-01
    • 2013-02-17
    • 2013-01-23
    • 2021-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多