-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
63 lines (54 loc) · 1.63 KB
/
server.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
import express from 'express';
import bodyParser from 'body-parser';
import { graphqlExpress } from 'apollo-server-express';
import compression from 'compression'
import next from 'next'
import cookieParser from 'cookie-parser'
import { makeSchema } from "graphql-spotify";
import { APOLLO_ENGINE_API_KEY } from './spotify-config'
import { Engine } from 'apollo-engine'
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
const port = parseInt(process.env.PORT, 10) || 3000
function attachEngine(server) {
const engine = new Engine({
engineConfig: {
apiKey: APOLLO_ENGINE_API_KEY
},
graphqlPort: port
});
engine.start();
server.use(engine.expressMiddleware())
}
async function start() {
await app.prepare()
const server = express();
server.use(compression())
server.use(cookieParser())
if (APOLLO_ENGINE_API_KEY) {
attachEngine(server)
}
// bodyParser is needed just for POST.
server.use(
'/graphql',
bodyParser.json(),
graphqlExpress(req => {
// this is set the client side via the the implicit Spotify Auth flow
const token = req.query['token']
const schema = makeSchema(token)
return {
schema,
tracing: true,
cacheControl: true
}
}));
server.get('*', (req, res) => {
return handle(req, res)
})
server.listen(port, (err) => {
if (err) throw err
if (dev) console.log(`> Ready on http://localhost:${port}`)
})
}
start()