-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserve.js
41 lines (28 loc) · 1.03 KB
/
serve.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
const express = require('express')
const graphqlHTTP = require('express-graphql')
const app = express()
const schema = require('./schema')
const fetch = require('node-fetch')
const util = require('util')
const DataLoader = require('dataloader')
const parseXml = util.promisify(require('xml2js').parseString)
const fetchAuthor = id => fetch(`https://www.goodreads.com/author/show.xml?id=${id}&key=02BwEOStDUpVLAoHbkO1g`)
.then(response => response.text())
.then(parseXml)
const fetchBook = id => fetch(`https://www.goodreads.com/book/show/${id}.xml?key=02BwEOStDUpVLAoHbkO1g`)
.then(response => response.text())
.then(parseXml)
app.use('/graphql', graphqlHTTP(req => {
const authorLoader = new DataLoader(keys => Promise.all(keys.map(fetchAuthor)))
const bookLoader = new DataLoader(keys => Promise.all(keys.map(fetchBook)))
return {
schema,
context: {
authorLoader,
bookLoader
},
graphiql: true
}
}))
app.listen(4000)
console.log('Listneing at port 4000...')