-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·70 lines (58 loc) · 1.69 KB
/
index.js
File metadata and controls
executable file
·70 lines (58 loc) · 1.69 KB
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
require('dotenv').config({ path: './.env' });
const { ApolloServer } = require('apollo-server-express');
const cookieParser = require('cookie-parser');
const { createServer } = require('http');
const jwt = require('jsonwebtoken');
const express = require('express');
const { isAuth, populateUser, errorHandler } = require('./src/middleware/index');
const { prisma, client } = require('./src/db');
const schema = require('./src/schema');
const apolloServer = new ApolloServer({
schema,
context: async ({ req, connection }) => {
if (connection) {
const { token } = connection.context;
try {
const { userId } = jwt.verify(token, process.env.APP_SECRET);
return { userId, subscription: prisma.subscription };
} catch (e) {
console.log(e, 'error decoding token');
}
} else {
return {
...req,
client,
prisma,
query: prisma.query,
mutation: prisma.mutation,
subscription: prisma.subscription
};
}
},
playground: true,
introspection: true,
debug: true,
subscriptions: {
onConnect: (connectionParams, webSocket, context) => {
const token = context.request.headers.cookie.slice(6);
return { token };
},
keepAlive: 10000
}
});
const corsConfig = {
origin: ['http://localhost:3000', 'https://www.up4.life', 'https://up4.life'],
credentials: true
};
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(isAuth);
app.use(populateUser);
app.use(errorHandler);
apolloServer.applyMiddleware({ app, cors: corsConfig, path: '/' });
const server = createServer(app);
apolloServer.installSubscriptionHandlers(server);
server.listen(process.env.PORT || 4000, () => {
console.log(`🚀 Server ready!!`);
});