axios - 使用 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来说仍然很新,很抱歉
解决方案
好吧,看起来我做了经典的承诺处理错误。我需要显式返回 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
});
},
},
};
推荐阅读
- sql-server - 无法在 AWS RDS 的 SQL Server 上执行系统过程
- python - 在python上导入模块的问题
- c# - Web API:控制器中几乎相同操作的巨大 TTFB 时间差
- python - 计数直到字典列表中的特定条件提供值的平均值
- python - 绘制数据点
- java - 为什么我不能用 Autowire 初始化 bean 超过 2 次?与 Vaadin 共度春天
- android - 无法插入记录 FireBase
- java - 玩 FTB Unleashed 时 Java 无法写入 Core Dump
- matlab - 如何使用 ode45 [MATLAB] 保存输出变量?
- html - Flebox 一项中心,第二端