-
Notifications
You must be signed in to change notification settings - Fork 1
/
resolvers.js
158 lines (134 loc) · 5.19 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Resolvers define the technique for fetching the types in the
// schema. We'll retrieve books from the "books" array above.
const imageMap = {
"person": "/assets/img/characters/ID.jpg",
"planet": "/assets/img/planets/ID.jpg",
"vehicle": "/assets/img/vehicles/ID.jpg",
"starship": "/assets/img/starships/ID.jpg",
"species": "/assets/img/species/ID.jpg",
}
const imageHost = 'https://starwars-visualguide.com'
const itemsPerPage = 10
const createId = (method, id) => {
const kind = getKind(method)
const uid = kind.split('').map((s) => s.charCodeAt(0)).reduce((n, c) => (n * c),1).toString(16)
return `${uid}::${id}`
}
const getKind = (method) => method.replace('List', '').replace('get', '').toLowerCase()
const getImageUrl = function (method, id) {
const kind = getKind(method)
return imageHost + imageMap[kind].replace('ID', id)
}
const getItemListWithID = async (dataSources, getMethod, page = 1) => {
const data = await dataSources.starWars[getMethod](page)
const count = data.count
const items = data.results.map(item => {
const id = item.url.match(/\d+\d*/)[0]
item.id = createId(getMethod, id)
item.picture = getImageUrl(getMethod, id)
return item
})
return {
count,
page,
items
}
}
const getListMeta = async (dataSources, getMethod) => {
const data = await dataSources.starWars[getMethod](1)
const count = data.count
return {
count,
itemsPerPage
}
}
const getItemWithID = async (dataSources, getMethod, id) => {
const [ hash, parsedId ] = id.split('::')
const data = await dataSources.starWars[getMethod](parsedId)
data.id = id
data.picture = getImageUrl(getMethod, parsedId)
return data
}
const getList = async (restUrls, getMethod, dataSources, args) => {
const ids = restUrls.map(url => url.match(/\d+\d*/)[0]).map(s => createId(getMethod, s))
const count = ids.length
const { first, offset } = args
const data = ids.slice(offset, offset + first).map(async (id) => {
return await getItemWithID(dataSources, getMethod, id)
})
return data
}
const getSubListCount = async (restUrls, metaField) => {
const count = restUrls.length
return count
}
const getItem = async (restUrl, getMethod, dataSources) => {
if (!restUrl) { return {} }
const id = restUrl.match(/\d+\d*/)[0]
return await getItemWithID(dataSources, getMethod, createId(getMethod, id))
}
module.exports = {
Query: {
Person: (_, { id }, { dataSources }) => {
return getItemWithID(dataSources, 'getPerson', id)
},
personPages: (_, { page }, { dataSources }) => {
return getItemListWithID(dataSources, 'getPersonList', page)
},
Starship: (_, { id }, { dataSources }) => {
return getItemWithID(dataSources, 'getStarship', id)
},
starshipPages: (_, { page }, { dataSources }) => {
return getItemListWithID(dataSources, 'getStarshipList', page)
},
Vehicle: (_, { id }, { dataSources }) => {
return getItemWithID(dataSources, 'getVehicle', id)
},
vehiclePages: (_, { page }, { dataSources }) => {
return getItemListWithID(dataSources, 'getVehicleList', page)
},
Planet: (_, { id }, { dataSources }) => {
return getItemWithID(dataSources, 'getPlanet', id)
},
planetPages: (_, { page }, { dataSources }) => {
return getItemListWithID(dataSources, 'getPlanetList', page)
},
Species: (_, { id }, { dataSources }) => {
return getItemWithID(dataSources, 'getSpecies', id)
},
speciesPages: (_, { page }, { dataSources }) => {
return getItemListWithID(dataSources, 'getSpeciesList', page)
},
},
Person: {
starships: ({ starships }, { first, offset }, { dataSources }) => getList(starships, 'getStarship', dataSources, { first, offset }),
starshipsCount: ({ starships }) => getSubListCount(starships, '_starshipsMeta'),
homeworld: ({ homeworld }, _, { dataSources }) => getItem(homeworld, 'getPlanet', dataSources),
species: ({ species }, { first, offset }, { dataSources }) => getList(species, 'getSpecies', dataSources, { first, offset }),
speciesCount: ({ species }) => getSubListCount(species, '_speciesMeta'),
vehicles: ({ vehicles }, { first, offset }, { dataSources }) => getList(vehicles, 'getVehicle', dataSources, { first, offset }),
vehiclesCount: ({ vehicles }) => getSubListCount(vehicles, '_vehiclesMeta'),
},
Starship: {
pilots: ({ pilots }, { first, offset }, { dataSources }) => getList(pilots, 'getPerson', dataSources, { first, offset }),
pilotsCount: ({ pilots }) => getSubListCount(pilots, '_pilotsMeta'),
},
Vehicle: {
pilots: ({ pilots }, { first, offset }, { dataSources }) => getList(pilots, 'getPerson', dataSources, { first, offset }),
pilotsCount: ({ pilots }) => getSubListCount(pilots, '_pilotsMeta'),
},
Planet: {
residents: ({ residents }, { first, offset }, { dataSources }) => getList(residents, 'getPerson', dataSources, { first, offset }),
residentsCount: ({ residents }) => getSubListCount(residents, '_residentsMeta'),
},
Species: {
homeworld: ({ homeworld }, _, { dataSources }) => getItem(homeworld, 'getPlanet', dataSources),
people: ({ people }, { first, offset }, { dataSources }) => getList(people, 'getPerson', dataSources, { first, offset }),
peopleCount: ({ people }) => getSubListCount(people, '_peopleMeta'),
},
Node: {
__resolveType(obj, context, info) {
return null;
}
}
};