forked from clayallsopp/graphqlhub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hn.js
414 lines (395 loc) · 10.8 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import {
getItem,
getUser,
getTopStoryIds,
getNewStoryIds,
getAskStoryIds,
getShowStoryIds,
getJobStoryIds
} from './apis/hn';
import {
graphql,
GraphQLSchema,
GraphQLObjectType,
GraphQLUnionType,
GraphQLString,
GraphQLNonNull,
GraphQLInt,
GraphQLEnumType,
GraphQLBoolean,
GraphQLList
} from 'graphql';
let getItems = function(ids, { offset, limit }) {
if (!ids || ids.length == 0) {
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 kidsField = (kidType) => ({
type : new GraphQLList(new GraphQLNonNull(kidType)),
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 });
}
});
let itemFields = () => ({
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 : new GraphQLNonNull(GraphQLInt),
description : 'The story\'s score, or the votes for a pollopt.'
},
title : {
type : new GraphQLNonNull(GraphQLString),
description : 'The title of the story, poll or job.',
},
parent : {
type : new GraphQLNonNull(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 : new GraphQLNonNull(GraphQLInt),
description : 'In the case of stories or polls, the total comment count.'
}
});
let toNonNull = (type) => ({
...type,
type: new GraphQLNonNull(type.type)
});
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: itemFields
});
const commentType = new GraphQLObjectType({
name: 'HackerNewsComment',
fields: () => ({
id: itemFields().id,
by: itemFields().by,
parent: itemFields().parent,
text: toNonNull(itemFields().text),
time: itemFields().time,
timeISO: itemFields().timeISO,
kids: kidsField(commentType),
deleted: itemFields().deleted,
dead: itemFields().dead,
})
});
let storyType = new GraphQLObjectType({
name: "Story",
fields: () => ({
id: itemFields().id,
by: itemFields().by,
descendants: itemFields().descendants,
score: itemFields().score,
time: itemFields().time,
timeISO: itemFields().timeISO,
title: itemFields().title,
url: itemFields().url,
text: itemFields().text,
kids: kidsField(commentType),
deleted: itemFields().deleted,
dead: itemFields().dead,
}),
});
const polloptType = new GraphQLObjectType({
name: 'HackerNewsPollopt',
fields: () => ({
id: itemFields().id,
by: itemFields().by,
score: itemFields().score,
time: itemFields().time,
timeISO: itemFields().timeISO,
text: toNonNull(itemFields().text),
parent: itemFields().parent,
deleted: itemFields().deleted,
})
});
const pollType = new GraphQLObjectType({
name: 'Poll',
fields: () => ({
id: itemFields().id,
by: itemFields().by,
descendants: itemFields().descendants,
score: itemFields().score,
time: itemFields().time,
timeISO: itemFields().timeISO,
title: itemFields().title,
text: itemFields().text,
kids: kidsField(commentType),
deleted: itemFields().deleted,
dead: itemFields().dead,
parts : {
type : new GraphQLList(new GraphQLNonNull(polloptType)),
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);
}
},
})
});
const jobType = new GraphQLObjectType({
name: 'Job',
fields: () => ({
id: itemFields().id,
by: itemFields().by,
score: itemFields().score,
title: itemFields().title,
time: itemFields().time,
timeISO: itemFields().timeISO,
jobUrl: {
...toNonNull(itemFields().url),
resolve: obj => obj.url
},
deleted: itemFields().deleted,
dead: itemFields().dead,
})
});
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 : itemFields().id,
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 topLevelItemType = new GraphQLUnionType({
name: 'TopLevelItem',
types: [ storyType, pollType, jobType ],
resolveType: function(value) {
if (value.type == "story") {
return storyType;
} else if (value.type == "poll") {
return pollType;
} else if (value.type == "job") {
return jobType;
} else {
return undefined;
}
}
});
let topLevelItemListType = function(bulkAPICall, description) {
return {
type : new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(topLevelItemType))),
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 : topLevelItemListType(getTopStoryIds, 'Up to 500 of the top stories'),
newStories : topLevelItemListType(getNewStoryIds, 'Up to 500 of the newest stories'),
showStories : topLevelItemListType(getShowStoryIds, 'Up to 200 of the Show HN stories'),
askStories : topLevelItemListType(getAskStoryIds, 'Up to 200 of the Ask HN stories'),
jobStories : topLevelItemListType(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;