我不确定我在这里做错了什么,但当我在graphql上运行查询时,我总是得到null。
这是我的解析器:
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
}
}
}
};
我不确定我是否在构建错误的东西。我尝试了另一个模式,它在下面,不管怎样,它仍然为null。
extend type Query {
countrytimelinedata: [countrytimelinedata]
timelineitems: [TimelineItem]
}
谢谢你的帮助,很抱歉,如果这是显而易见的,仍然是graphQL的新手
看起来我犯了经典的promise处理错误。我需要显式返回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
});
},
},
};