-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
48 lines (44 loc) · 1.27 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
require('dotenv/config');
require('express-async-errors');
const express = require('express');
const { ApolloServer } = require('apollo-server-express');
const resolvers = require('./Resolvers');
const typeDefs = require('./TypeDefs');
const { connection } = require('./Database/utilis');
const { verifyUser } = require('./Helper/Context');
const error = require('./Middlewares/error');
const cors = require('cors');
const apolloServer = new ApolloServer({
typeDefs,
resolvers,
context: async ({ req }) => {
await verifyUser(req)
return {
email: req.email,
loggedInUserId: req.loggedInUserId
}
},
formatError: (err) => {
console.log(err.GraphQLError);
return err;
}
});
async function startServer() {
const app = express();
app.use(express.json());
app.use(cors());
await apolloServer.start();
apolloServer.applyMiddleware({ app });
app.use('/', (req, res) => {
res.send({
message: "Hwllo World"
})
})
const port = process.env.PORT || 3001
connection();
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
console.log(`Graphql Endpoint is running on ${apolloServer.graphqlPath}`);
})
}
startServer();