-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.mjs
94 lines (82 loc) · 1.4 KB
/
schema.mjs
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
import { buildSchema } from 'graphql';
const THREE_HUNDRED_MS = 300;
const USERS = [
{
"id": "0",
"name": "Brandon",
"cityId": "a"
},
{
"id": "1",
"name": "Nafeu",
"cityId": "b"
},
{
"id": "2",
"name": "Saba",
"cityId": "c"
}
];
const CITIES = [
{
"id": "a",
"name": "Smith's Falls",
"population": 8780
},
{
"id": "b",
"name": "Toronto",
"population": 2930000
},
{
"id": "c",
"name": "Chicago",
"population": 2710000
}
]
const fetchUsers = () => new Promise(resolve => {
setTimeout(() => {
resolve(USERS)
}, THREE_HUNDRED_MS)
});
const fetchCities = () => new Promise(resolve => {
setTimeout(() => {
resolve(CITIES)
}, THREE_HUNDRED_MS)
});
export const schema = buildSchema(`
type Query {
getUsers: [User]
}
type User {
id: String
name: String
city: City
}
type City {
id: String
name: String
population: Int
}
`);
export const rootValue = {
getUsers: async () => {
const users = await fetchUsers();
const cities = await fetchCities();
const getCityById = cityId => {
for (const city of cities) {
if (city.id === cityId) {
return city;
}
}
}
let output = [...users];
output = output.map(user => {
return {
...user,
city: getCityById(user.cityId)
}
});
return output;
}
}