-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
96 lines (86 loc) · 2.85 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
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
// eslint-disable prevent-abbreviations
require('dotenv').config();
const http = require('http');
const { ApolloServer, ValidationError } = require('apollo-server-express');
const { typeDefs, resolvers } = require('./graphql');
const { models, sequelize } = require('./components');
const app = require('./app');
const storeUpload = require('./store-upload');
const pubsub = require('./pubsub');
const userCtrl = require('./components/user/user.controller');
const server = new ApolloServer({
uploads: false,
typeDefs,
resolvers,
context: async ({ req, connection }) => {
let token = null;
let user = null;
let isAuthenticated = false;
// console.log('context', req);
if (connection) {
// Operation is a Subscription
token = connection.context.token;
} else if (req && req.headers.authorization) {
// Operation is a Query/Mutation
// Obtain header-provided token from req.headers
token = req.headers.authorization;
}
if (token) {
user = await userCtrl.isAuthenticated(token);
if (user) isAuthenticated = true;
}
return {
isAuthenticated,
user,
req,
models,
sequelize,
pubsub,
storeUpload
};
},
subscriptions: {
path: '/subscriptions',
// eslint-disable-next-line unicorn/prevent-abbreviations
onConnect: async (connectionParams, _webSocket, _context) => {
console.log('Client connected');
// console.log('connectionParams:', connectionParams);
if (connectionParams.token) {
return { token: connectionParams.token };
}
throw new ValidationError('Missing auth token!');
}
// onDisconnect: (_webSocket, _context) => {
// console.log('Client disconnected');
// }
}
});
server.applyMiddleware({ app, path: '/graphql' });
const httpServer = http.createServer(app);
server.installSubscriptionHandlers(httpServer);
const PORT = process.env.PORT || 5000;
sequelize
.sync({ alter: true })
.then(() => {
console.log('Database connected');
httpServer.listen(PORT, () => {
console.log(
`🚀 Server ready at http://localhost:${PORT}${server.graphqlPath}`
);
console.log(
`🚀 Subscriptions ready at ws://localhost:${PORT}${server.subscriptionsPath}`
);
});
})
.catch((error) => {
console.error('Unable to connect to the database:', error);
});
process.on('unhandledRejection', (error) => {
console.error(error);
console.log('Shutting Down the server...');
// safely close
httpServer.close(() => {
// kill
process.exit(1);
});
});