首页 > 解决方案 > 使用 Apollo 运行时以 null 形式出现的查询。我不确定这是解析程序还是架构问题

问题描述

我不确定我在这里做错了什么,但是当我在 graphql 上运行查询时,我一直为空。

这是我的解析器:

Query: {
    getCountryData: (parent, args) => {
      return axios
        .get(`https://thevirustracker.com/free-api?countryTimeline=US`)
        .then((res) => {
          console.log(res.data);
          res.data;
        });
    },
  },
};

这是我的架构

gql`
  extend type Query {
    getCountryData: getCountryData
  }
  type getCountryData {
    countrytimelinedata: [countrytimelinedata]
    timelineitems: [TimelineItem]
  }
  type TimelineItem {
    date: String!
    new_daily_cases: String!
    new_daily_deaths: String!
    total_cases: String!
    total_recoveries: String!
    total_deaths: String!
  }
  type countrytimelinedata {
    info: Info
  }
  type Info {
    ourid: String!
    title: String!
    code: String!
    source: String!
  }
`

这是我的查询

{
  getCountryData{
    countrytimelinedata{
      info{
        code
      }
    }
  }
};

我不确定我的结构是否不正确。我尝试了另一种模式,它在下面,无论如何,它仍然为空。

extend type Query {
    countrytimelinedata: [countrytimelinedata]
    timelineitems: [TimelineItem]
  }

感谢您的帮助,如果它很明显,对graphQL来说仍然很新,很抱歉

标签: axiosschemaapollo

解决方案


好吧,看起来我做了经典的承诺处理错误。我需要显式返回 res.data 对象

Query: {
    getCountryData: (parent, args) => {
      return axios
        .get(`https://thevirustracker.com/free-api?countryTimeline=US`)
        .then((res) => {
          console.log(res.data);
         return res.data; <<<< add return here
        });
    },
  },
};

推荐阅读