-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
80 lines (67 loc) · 1.79 KB
/
index.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
import { ApolloServer } from 'apollo-server-express';
import dotenv from 'dotenv';
import express from 'express';
import { buildContext } from 'graphql-passport';
import mongoose from 'mongoose';
import session from 'express-session';
import { v4 as uuidv4 } from 'uuid';
import typeDefs from './typeDefs';
import resolvers from './resolvers';
import passport from './passport/passport';
dotenv.config();
const {
CORS_ORIGIN,
MONGODB_URI,
MONGODB_USERNAME,
MONGODB_PASSWORD,
SESSION_SECRET
} = process.env;
mongoose.Promise = global.Promise;
mongoose.connect(MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
user: MONGODB_USERNAME,
pass: MONGODB_PASSWORD
});
// NOTE: To drop the entire database
// .then(conn => mongoose.connection.db.dropDatabase());
const PORT = process.env.PORT || 4300;
// TODO: Setup CORS policy to match deployed frontend
const corsOptions = {
origin: CORS_ORIGIN,
optionsSuccessStatus: 200,
credentials: true
};
const app = express();
// TODO: Recheck the arguments here
const sessionConfig = {
genid: req => uuidv4(),
secret: SESSION_SECRET,
resave: false,
saveUninitialized: false
};
if (app.get('env') === 'production') {
app.set('trust proxy', 1); // trust first proxy
sessionConfig.cookie = {
// serve secure cookies
secure: true,
sameSite: 'none'
};
}
app.use(session(sessionConfig));
// Passport middleware
app.use(passport.initialize());
app.use(passport.session());
const server = new ApolloServer({
typeDefs,
resolvers,
context: ({ req, res }) => buildContext({ req, res }),
playground: {
settings: {
'request.credentials': 'same-origin',
'editor.theme': 'light'
}
}
});
server.applyMiddleware({ app, cors: corsOptions });
app.listen(PORT, () => console.log(`Server ready at port ${PORT}`));