-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
39 lines (34 loc) · 1.05 KB
/
app.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 cors = require('cors');
const { graphqlHTTP } = require('express-graphql');
const logger = require('loglevel');
const graphqlSchema = require('./src/schema');
const graphqlResolver = require('./src/resolvers');
const isAuth = require('./src/middleware/is-auth');
const startServer = ({ port = process.env.PORT} = {}) => {
const app = express();
app.use(cors());
app.use(express.json());
app.use(isAuth);
app.use(
'/graphql',
graphqlHTTP({
schema: graphqlSchema,
rootValue: graphqlResolver,
graphiql: process.env.NODE_ENV !== 'production'
})
);
return new Promise(resolve => {
const server = app.listen(port, () => {
logger.info(`Listening on port ${server.address().port}`);
const originalClose = server.close.bind(server);
server.close = () => {
return new Promise(resolveClose => {
originalClose(resolveClose);
});
};
resolve(server);
});
});
};
module.exports = startServer;