-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
51 lines (42 loc) · 1.31 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
import { fileLoader, mergeResolvers, mergeTypes } from 'merge-graphql-schemas';
import { ApolloServer } from 'apollo-server-express';
import express from 'express';
import mongoose from 'mongoose';
import morgan from 'morgan';
import path from 'path';
import models from './models';
import { getUser } from './helper';
const app = express();
const graphqlEndpoint = '/graphql';
const dbName = 'test';
const typeDefs = mergeTypes(fileLoader(path.join(__dirname, './schema')));
const resolvers = mergeResolvers(fileLoader(path.join(__dirname, './resolvers')));
const db = mongoose.connection;
// eslint-disable-next-line no-console
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', () => {
app.listen({ port: 8081 }, () => {
// eslint-disable-next-line no-console
console.log(`🚀 Server ready at http://localhost:8081${graphqlEndpoint}`);
});
});
const server = new ApolloServer({
typeDefs,
resolvers,
tracing: true,
playground: {
settings: {
'editor.theme': 'light',
},
},
context: async ({ req }) => {
const me = await getUser(req);
return {
me,
model: models,
};
},
});
app.use(morgan('tiny'));
server.applyMiddleware({ app });
mongoose.connect(`mongodb://localhost/${dbName}`, { useNewUrlParser: true, useUnifiedTopology: true });