-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·37 lines (33 loc) · 1.04 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
/* eslint-disable no-console */
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,
useUnifiedTopology: 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) {
// Find or create user
currentUser = await findOrCreateUser(authToken);
}
} catch (err) {
console.error(`Unable to authenticate user with token ${authToken}`, err);
}
return { currentUser };
},
});
server.listen({ port: process.env.PORT || 4000 }).then(({ url }) => {
console.log(`Server is listening on ${url}`);
});