-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolvers.js
130 lines (124 loc) · 4.25 KB
/
resolvers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import '@babel/polyfill';
import axios from 'axios';
import params from './params';
const { baseURL, format, indicatorCodes } = params.api;
const sortByYear = (a, b) => {
if (a.date > b.date) {
return 1;
}
if (a.date < b.date) {
return -1;
}
return 0;
}
const resolvers = {
Query: {
allCountries: () => {
const url = `${baseURL}/?format=${format}&per_page=500`;
return axios.get(url)
.then((response) => {
if (response.data.length <= 1) {
return [];
}
return response.data[1].filter(country => country.capitalCity !== '')
.sort((a, b) => {
if (a.name > b.name) {
return 1;
}
if (a.name < b.name) {
return -1;
}
return 0;
});
})
.catch((error) => console.log(error))
},
country: (root, {iso2Code}) => {
const url = `${baseURL}/${iso2Code}/?format=${format}`;
return axios.get(url)
.then((response) => {
if (response.data.length <= 1) {
return {};
}
if (response.data[1].length === 0) {
return {};
}
return response.data[1][0];
})
.catch((error) => console.log(error))
},
countries: (root, { iso2Codes }) => {
const countries = iso2Codes.join(';');
const url = `${baseURL}/${countries}/?format=${format}`;
return axios.get(url)
.then((response) => {
if (response.data.length <= 1) {
return [];
}
if (response.data[1].length === 0) {
return [];
}
return response.data[1];
})
.catch((error) => { console.log(error); return []; })
}
},
Country: {
populations: (country) => {
const url = `${baseURL}/${country.iso2Code}/indicators/${indicatorCodes.population}?format=${format}&per_page=100`;
return axios.get(url)
.then((response) => {
if (response.data.length <= 1) {
return [];
}
return response.data[1].sort(sortByYear);
})
.catch((error) => console.log(error))
},
emissions: (country) => {
const url = `${baseURL}/${country.iso2Code}/indicators/${indicatorCodes.emission}?format=${format}&per_page=100`;
return axios.get(url)
.then((response) => {
if (response.data.length <= 1) {
return [];
}
return response.data[1].sort(sortByYear);
})
.catch((error) => console.log(error))
},
gdps: (country) => {
const url = `${baseURL}/${country.iso2Code}/indicators/${indicatorCodes.gdp}?format=${format}&per_page=100`;
return axios.get(url)
.then((response) => {
if (response.data.length <= 1) {
return [];
}
return response.data[1].sort(sortByYear);
})
.catch((error) => console.log(error))
},
years: async (country) => {
const populationUrl = `${baseURL}/${country.iso2Code}/indicators/${indicatorCodes.population}?format=${format}&per_page=100`;
const emissionUrl = `${baseURL}/${country.iso2Code}/indicators/${indicatorCodes.emission}?format=${format}&per_page=100`;
const gdpUrl = `${baseURL}/${country.iso2Code}/indicators/${indicatorCodes.gdp}?format=${format}&per_page=100`;
const populations = await axios.get(populationUrl);
const emissions = await axios.get(emissionUrl);
const gdps = await axios.get(gdpUrl);
// Combine years into single array of objects
const data = populations.data[1].reduce((results, { date, value }) => {
const emissionObject = emissions.data[1].find((emission) => emission.date === date);
const gdpObject = gdps.data[1].find((gdp) => gdp.date === date);
let year = {
date,
emission: ('value' in emissionObject && emissionObject.value !== null ? emissionObject.value.toFixed(2) : 0),
population: (value === null ? 0 : value),
gdp: ('value' in gdpObject && gdpObject.value !== null ? gdpObject.value.toFixed(2) : 0),
};
results.push(year);
return results;
}, []);
return data.sort(sortByYear);
}
}
};
export default resolvers;