-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.ts
35 lines (29 loc) · 974 Bytes
/
server.ts
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
const dotenv = require('dotenv');
dotenv.config();
import express from 'express';
import { ApolloServer } from 'apollo-server-express';
import depthLimit from 'graphql-depth-limit';
import { createServer } from 'http';
import compression from 'compression';
import cors from 'cors';
import schema from './graphql/schema';
import { MongoHelper } from './helpers/mongoHelpers';
const app = express();
const mHelper = new MongoHelper();
mHelper.initiateMongoConnection();
const server = new ApolloServer({
schema,
validationRules: [depthLimit(7)],
introspection: true,
playground: true,
context: async ({ req }) => {
return await mHelper.validateUser(req);
},
});
app.use('*', cors());
app.use(compression());
server.applyMiddleware({ app, path: '/graphql' });
const httpServer = createServer(app);
httpServer.listen({ port: process.env.PORT }, (): void =>
console.log(`\n🚀 GraphQL is now running on http://localhost:${process.env.PORT}/graphql`)
);