0

I am working on retrieving data from an API with graphql-yoga and node-fetch (in node.js). This is how my code looks like:

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&currency=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'));

The data you get when you run the data from the API endpoint I am using is as shown below:

{
    "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"
}

At the moment based on my code above, I can only retrieve the values held by the start_date and end_date fields when I run my query. However, I don't know how to create a type definition to retrieve the object under price because the field names are dates and they change every day. Please help me write a type definition and resolver for the query we will use to retrieve this data.

Ps: When running your tests you may come across some error codes. This should help you understand which error it is:

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
Leon Peter
  • 139
  • 1
  • 1
  • 7
  • GraphQL doesn't support the sort of data structure you're trying to implement. You should probably transform the response into a structure that *can* be represented in your schema. Otherwise you have to use a custom scalar as outlined in the linked dupe. – Daniel Rearden May 05 '20 at 13:16
  • @DanielRearden Great! using the custom scalar as advised above works. However, you mentioned that I could probably transform the response into a structure that can be represented in my schema. How would you go about doing this? – Leon Peter May 05 '20 at 13:46
  • For example: `price: [{ date: '2020-04-30', rates: [{ currency: 'EURUSD', values: { close: 1.09528 } }] }`. You'd iterate over the `price` object's keys and create the appropriate array from the values. – Daniel Rearden May 05 '20 at 13:56
  • Awesome, I get it. Thanks. – Leon Peter May 05 '20 at 14:30

0 Answers0