-
Notifications
You must be signed in to change notification settings - Fork 46
/
hn.js
295 lines (287 loc) · 7.98 KB
/
hn.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import {
getItem,
getUser,
getTopStoryIds,
getNewStoryIds,
getAskStoryIds,
getShowStoryIds,
getJobStoryIds
} from './apis/hn';
import {
graphql,
GraphQLSchema,
GraphQLObjectType,
GraphQLString,
GraphQLNonNull,
GraphQLInt,
GraphQLEnumType,
GraphQLBoolean,
GraphQLList
} from 'graphql';
let getItems = function(ids, { offset, limit }) {
if (!ids) {
ids = [];
}
let promises = (ids.slice(offset, offset + limit)).map((id) => {
return getItem(id)
});
return Promise.all(promises);
}
let itemTypeEnum = new GraphQLEnumType({
name: 'ItemType',
description: 'The type of item',
values: {
job: {
value: 'job'
},
story: {
value: 'story'
},
comment: {
value: 'comment'
},
poll: {
value: 'poll'
},
pollopt: {
value: 'pollopt'
}
}
});
let itemType = new GraphQLObjectType({
name : 'HackerNewsItem',
description : 'Stories, comments, jobs, Ask HNs and even polls are just items. They\'re identified by their ids, which are unique integers',
fields: () => ({
id : {
type : new GraphQLNonNull(GraphQLString),
description : 'The item\'s unique id.',
resolve : (item) => item.id.toString()
},
deleted : {
type : GraphQLBoolean,
description : 'if the item is deleted'
},
type : {
type : new GraphQLNonNull(itemTypeEnum),
description: 'The type of item. One of "job", "story", "comment", "poll", or "pollopt".'
},
by : {
type : new GraphQLNonNull(userType),
description : 'The item\'s author.',
resolve : (item) => {
return getUser(item.by);
}
},
time : {
type : new GraphQLNonNull(GraphQLInt),
description : 'Creation date of the item, in Unix Time.'
},
timeISO : {
type : new GraphQLNonNull(GraphQLString),
description : 'Creation date of the item, in ISO8601',
resolve : (item) => {
let date = new Date(item.time * 1000);
return date.toISOString();
}
},
text : {
type : GraphQLString,
description : 'The comment, story or poll text. HTML.'
},
dead : {
type : GraphQLBoolean,
description : 'if the item is dead'
},
url : {
type : GraphQLString,
description : 'The URL of the story.'
},
score : {
type : GraphQLInt,
description : 'The story\'s score, or the votes for a pollopt.'
},
title : {
type : GraphQLString,
description : 'The title of the story, poll or job.'
},
kids : {
type : new GraphQLList(itemType),
description : 'The item\'s comments, in ranked display order.',
args : {
limit : {
description : 'Number of items to return',
type : GraphQLInt,
},
offset : {
description : 'Initial offset of number of items to return',
type : GraphQLInt,
}
},
resolve : (item, { offset = 0, limit = 10 } = {}) => {
return getItems(item.kids, { offset, limit });
}
},
parent : {
type : itemType,
description : 'The item\'s parent. For comments, either another comment or the relevant story. For pollopts, the relevant poll.',
resolve : (item) => {
if (!item.parent) {
return null;
}
return getItem(item.parent);
}
},
parts : {
type : new GraphQLList(itemType),
description : 'A list of related pollopts, in display order.',
resolve : (item) => {
if (!item.parts) {
return null;
}
let promises = item.parts.map((partId) => {
return getItem(partId)
});
return Promise.all(promises);
}
},
descendants : {
type : GraphQLInt,
description : 'In the case of stories or polls, the total comment count.'
}
})
});
let userType = new GraphQLObjectType({
name : 'HackerNewsUser',
description : 'Users are identified by case-sensitive ids. Only users that have public activity (comments or story submissions) on the site are available through the API.',
fields : {
id : {
type : new GraphQLNonNull(GraphQLString),
description : 'The user\'s unique username. Case-sensitive. Required.'
},
delay : {
type : new GraphQLNonNull(GraphQLInt),
description : 'Delay in minutes between a comment\'s creation and its visibility to other users.'
},
created : {
type : new GraphQLNonNull(GraphQLInt),
description : 'Creation date of the user, in Unix Time.'
},
createdISO : {
type : new GraphQLNonNull(GraphQLString),
description : 'Creation date of the user, in ISO8601',
resolve : (user) => {
let date = new Date(user.created * 1000);
return date.toISOString();
}
},
about : {
type : GraphQLString,
description : 'The user\'s optional self-description. HTML.'
},
submitted : {
type : new GraphQLList(itemType),
description: 'List of the user\'s stories, polls and comments.',
args : {
limit : {
description : 'Number of items to return',
type : GraphQLInt,
},
offset : {
description : 'Initial offset of number of items to return',
type : GraphQLInt,
}
},
resolve : (user, { limit = 10, offset = 0 } = {}) => {
let submitted = user.submitted;
return getItems(submitted, { limit, offset });
}
}
}
});
let createBulkType = function(bulkAPICall, description) {
return {
type : new GraphQLList(itemType),
description,
args : {
limit : {
description : 'Number of items to return',
type : GraphQLInt,
},
offset : {
description : 'Initial offset of number of items to return',
type : GraphQLInt,
}
},
resolve: function(root, { limit = 30, offset = 0 } = {}) {
return bulkAPICall().then((ids) => {
return getItems(ids, { limit, offset });
});
}
}
}
let hnType = new GraphQLObjectType({
name : 'HackerNewsAPI',
description : 'The Hacker News V0 API',
fields : {
item : {
type : itemType,
args : {
id : {
description : 'id of the item',
type: new GraphQLNonNull(GraphQLInt),
}
},
resolve: function(root, { id }) {
return getItem(id);
}
},
user : {
type: userType,
args : {
id : {
description : 'id of the user',
type: new GraphQLNonNull(GraphQLString),
}
},
resolve: function(root, { id }) {
return getUser(id);
}
},
topStories : createBulkType(getTopStoryIds, 'Up to 500 of the top stories'),
newStories : createBulkType(getNewStoryIds, 'Up to 500 of the newest stories'),
showStories : createBulkType(getShowStoryIds, 'Up to 200 of the Show HN stories'),
askStories : createBulkType(getAskStoryIds, 'Up to 200 of the Ask HN stories'),
jobStories : createBulkType(getJobStoryIds, 'Up to 200 of the Job stores'),
stories : {
type : new GraphQLList(itemType),
description : 'Return list of stories',
args : {
limit : {
description : 'Number of items to return',
type : GraphQLInt,
},
offset : {
description : 'Initial offset of number of items to return',
type : GraphQLInt,
},
storyType : {
description : 'Type of story to list',
type : new GraphQLNonNull(GraphQLString)
}
},
resolve: function(root, { limit = 30, offset = 0, storyType } = {}) {
let bulkAPICall = {
top : getTopStoryIds,
show : getShowStoryIds,
new : getNewStoryIds,
ask : getAskStoryIds,
job : getJobStoryIds
}[storyType];
return bulkAPICall().then((ids) => {
return getItems(ids, { limit, offset });
});
}
}
}
})
export const QueryObjectType = hnType;