我正在使用graphql-yoga和node-fetch(在node.js中)从API检索数据。这是我的代码的样子:
const {GraphQLServer} = require('graphql-yoga');
const fetch = require('node-fetch');
const typeDefs = `
type Query {
getTimeseries: Timeseries
}
type Timeseries {
end_date: String
start_date: String
}
`;
const resolvers = {
Query: {
getTimeseries: async (_, {}) => {
const response = await fetch(`https://fxmarketapi.com/apitimeseries?api_key=48Ki5q0sTKFQR3fuTvHw¤cy=EURUSD,GBPUSD&interval=daily&start_date=2020-04-30&end_date=2020-05-01&format=ohlc`);
return response.json();
},
},
};
const server = new GraphQLServer({
typeDefs,
resolvers
});
server.start(() => console.log('Server is running on localhost:4000'));
从我正在使用的API端点运行数据时获得的数据如下所示:
{
"end_date": "2020-05-01",
"price": {
"2020-04-30": {
"EURUSD": {
"close": 1.09528,
"high": 1.09726,
"low": 1.08331,
"open": 1.08735
},
"GBPUSD": {
"close": 1.25951,
"high": 1.26432,
"low": 1.24292,
"open": 1.24671
}
},
"2020-05-03": {
"EURUSD": {
"close": 1.09802,
"high": 1.10152,
"low": 1.09348,
"open": 1.09528
},
"GBPUSD": {
"close": 1.24941,
"high": 1.25993,
"low": 1.24859,
"open": 1.25918
}
}
},
"start_date": "2020-04-30"
}
基于上面的代码,当我运行查询时,我只能检索start_date
和end_date
字段所保存的值。但是,我不知道如何创建类型定义来检索price
下的对象,因为字段名称是日期,并且每天都在变化。请帮助我为将用于检索此数据的查询编写类型定义和解析器。
Ps:运行测试时,您可能会遇到一些错误代码。这应该可以帮助您了解它是哪个错误:
202 - Requested currency code invalid or not in plan for Live and Convert Endpoints
203 - Requested currency code invalid or not in plan for the Historical, Change, Timeseries and Pandas Endpoints
301 - Invalid date type
302 - Requested date is a weekend applicable to Historical, Change, Timeseries and Pandas Endpoints
401 - Api_Key is invalid
501 - Requestout of Range
502 - Interval Parameters invalid