-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
52 lines (43 loc) · 1.13 KB
/
app.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
const express = require('express');
const { ApolloServer, gql } = require('apollo-server-express');
const { importSchema } = require('graphql-import');
const bodyParser = require('body-parser');
const cors = require('cors');
const dotenv = require('dotenv');
const mongoose = require('mongoose');
// Dotenv config
dotenv.config();
// Database connection
mongoose.connect(
process.env.MONGO_CONNECTION,
{ useNewUrlParser: true }
);
mongoose.Promise = global.Promise;
// Express Instance
const app = express();
// Graphql Schema
const typeDefs = gql`
${importSchema('./schema/schema.graphql')}
`;
// Graphql Resolvers
const resolvers = require('./schema/resolvers.js');
// Apollo Server Instance
const server = new ApolloServer({
typeDefs,
resolvers,
playground: {
settings: {
'editor.theme': 'light',
},
},
});
// Express Middlewares
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cors());
// Apollo Server Middleware
server.applyMiddleware({ app });
// Run the API
app.listen(process.env.API_PORT, () => {
console.log(`API is running on port ${process.env.API_PORT}`);
});