-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
87 lines (64 loc) · 2.01 KB
/
index.js
File metadata and controls
87 lines (64 loc) · 2.01 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { ApolloServer } from "@apollo/server"
import { expressMiddleware } from '@apollo/server/express4';
import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer';
import express from 'express';
import http from 'http';
import cors from 'cors';
import dotenv from "dotenv";
import passport from "passport";
import session from "express-session";
import connectMongo from "connect-mongodb-session"
import {buildContext } from "graphql-passport";
import mergedResolvers from "./resolvers/index.js"
import mergedTypeDefs from "./typeDefs/index.js"
import { connectDB } from "./db/connectDB.js";
import { configurePassport } from "./passport/passport.config.js";
dotenv.config();
configurePassport();
const app = express();
const httpServer = http.createServer(app);
const MongoDBStore=connectMongo(session);
const store = new MongoDBStore({
uri: process.env.MONGO_URI,
collection: 'sessions',
});
store.on("error",(err)=>{
console.error(err);
});
app.use(
session({
secret:process.env.SESSION_SECRET,
resave:false,
saveUninitialized:false,
cookie:{
maxAge:1000*60*60*24,
httpOnly:true,
},
store:store,
})
)
app.use(passport.initialize());
app.use(passport.session());
const server = new ApolloServer({
typeDefs:mergedTypeDefs,
resolvers:mergedResolvers,
plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
})
await server.start();
app.use(
'/graphql',
cors({
origin:"http://localhost:3000",
credentials:true,
}),
express.json(),
// expressMiddleware accepts the same arguments:
// an Apollo Server instance and optional configuration options
expressMiddleware(server, {
context: async ({ req,res }) => buildContext({ req,res }),
}),
);
// Modified server startup
await new Promise((resolve) => httpServer.listen({ port: 4000 }, resolve));
await connectDB();
console.log(`🚀 Server ready at http://localhost:4000/graphql`);