-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathserve.js
39 lines (32 loc) · 1003 Bytes
/
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
const express = require('express')
const graphqlHTTP = require('express-graphql')
const app = express()
const fetch = require('node-fetch')
const schema = require('./schema')
const DataLoader = require('dataloader')
const util = require('util')
const parseXML = util.promisify(require('xml2js').parseString)
const fetchAuthor = id =>
fetch(`https://www.goodreads.com/author/show.xml?id=${id}&key=Rz607gLuIzr1a55wEbl3w `)
.then(response => response.text())
.then(parseXML)
const fetchBook = id =>
fetch(`https://www.goodreads.com/book/show/${id}.xml?key=Rz607gLuIzr1a55wEbl3w`)
.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('Listening ...')