-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
33 lines (30 loc) · 895 Bytes
/
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
const { ApolloServer } = require("apollo-server");
const mongoose = require("mongoose");
const typeDefs = require("./typeDefs");
const resolvers = require("./resolvers");
const { findOrCreateUser } = require("./controllers/userController");
require("dotenv").config();
mongoose
.connect(process.env.MONGO_URI, { useNewUrlParser: true })
.then(_ => {
console.log("DB connected");
})
.catch(err => console.error(err));
const server = new ApolloServer({
typeDefs,
resolvers,
context: async ({ req }) => {
let authToken = null;
let currentUser = null;
try {
authToken = req.headers.authorization;
if (authToken) {
currentUser = await findOrCreateUser(authToken);
}
} catch {
console.error(`Unable to authenticate user with token ${authToken}`);
}
return { currentUser };
}
});
server.listen().then(({ url }) => console.log(`Server is listening on ${url}`));