-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
executable file
·80 lines (73 loc) · 1.88 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
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
const { ApolloServer } = require('apollo-server');
const mongoose = require('mongoose');
const typeDefs = require('./typeDefs');
const resolvers = require('./resolvers');
const userLoader = require('./dataloader');
const { MemcachedCache } = require('apollo-server-cache-memcached');
require('dotenv').config();
const { findOrCreateUser } = require('./controllers/UserController');
mongoose.set('debug', true);
mongoose.set('useFindAndModify', false);
mongoose
.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => console.log('DB Connectd'))
.catch((e) => console.log(e));
const authContext = (connection, req) => {
let authToken = null;
let currenttUser = null;
if (connection) {
connection.connect;
} else {
try {
authToken = req.headers.authorization;
if (authToken) {
{
/* find a user or create a user */
currenttUser = findOrCreateUser(authToken);
}
}
} catch (err) {
console.error(`Unable to authenticate user with token ${authToken}`, err);
}
return { currenttUser };
}
};
const server = new ApolloServer({
typeDefs,
resolvers,
onHealthCheck: () => {
return new Promise((resolve, reject) => {
// Replace the `true` in this conditional with more specific checks!
if (false) {
resolve();
} else {
reject();
}
});
},
context: async ({ req, connection }) => ({
...authContext(connection, req),
loaders: { userLoader }
}),
cors: {
credentials: true,
origin: (origin, callback) => {
const whitelist = [
'http://localhost:3000',
'https://geopins.ashokhein.me',
'https://geopins-62q92papc.vercel.app'
];
if (whitelist.indexOf(origin) !== -1) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
}
}
});
server.listen({ port: process.env.PORT || 4000 }).then(({ url }) => {
console.log(`Server is listening on ${url}`);
});