This repository has been archived by the owner on Nov 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.ts
114 lines (102 loc) · 3.03 KB
/
server.ts
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
import express from 'express';
import http from 'http';
import bodyParser from 'body-parser';
import { graphqlExpress, graphiqlExpress } from 'apollo-server-express';
import { makeExecutableSchema } from 'graphql-tools';
import { SubscriptionServer } from 'subscriptions-transport-ws';
import { execute, subscribe, GraphQLSchema } from 'graphql';
import cors from 'cors';
import { logger } from '../../utils';
import { findUserByUserType, db } from './db';
import { resolvers } from './resolvers';
import { typeDefs } from './typeDefs';
import { config } from '../../config';
function auth(req, res, next) {
const { authorization } = req.headers;
const parts = authorization.split(' ');
const bearer = parts[0];
const credential = parts[1];
if (/^Bearer$/i.test(bearer) && credential) {
req.user = {
userType: credential
};
}
next();
}
const schema: GraphQLSchema = makeExecutableSchema({ typeDefs, resolvers });
function start() {
const app = express();
const server = http.createServer(app);
server.listen(config.PORT, err => {
if (err) {
throw new Error(err);
}
logger.info(`Go to ${config.GRAPHQL_ENDPOINT} to run queries!`);
const ss = new SubscriptionServer(
{
execute,
subscribe,
schema,
onConnect: (connectionParams, webSocket, context) => {
logger.info('onConnect');
logger.info({ label: 'connectionParams', msg: connectionParams });
const { token } = connectionParams;
const parts = token.split(' ');
const bearer = parts[0];
const credential = parts[1];
if (/^Bearer$/i.test(bearer) && credential) {
const subscribeUser = findUserByUserType(credential);
return {
user: subscribeUser
};
}
throw new Error('Missing auth token!');
},
onOperation: (message, params, webSocket) => {
// https://github.com/apollographql/subscriptions-transport-ws/issues/300
logger.info('onOperation');
logger.info({ label: 'params', msg: params });
logger.info({ label: 'message', msg: message });
return {
...params,
context: {
...params.context,
db
}
};
},
onOperationComplete: webSocket => {
logger.info('onOperationComplete');
},
onDisconnect: (webSocket, context) => {
logger.info('onDisconnect');
}
},
{
server,
path: config.WEBSOCKET_ROUTE
}
);
});
app.use(cors(), auth);
app.use(
config.GRAPHQL_ROUTE,
bodyParser.json(),
graphqlExpress((req?: express.Request) => {
return {
schema,
context: {
user: (req as any).user
}
};
})
);
app.use(
config.GRAPHIQL_ROUTE,
graphiqlExpress({ endpointURL: config.GRAPHQL_ROUTE, subscriptionsEndpoint: config.WEBSOCKET_ENDPOINT })
);
}
if (process.env.NODE_ENV !== 'test') {
start();
}
export { start };