-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathindex.js
More file actions
109 lines (91 loc) · 3.07 KB
/
Copy pathindex.js
File metadata and controls
109 lines (91 loc) · 3.07 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import express from 'express';
import { graphqlExpress, graphiqlExpress } from 'graphql-server-express';
import { createServer } from 'http';
import { SubscriptionServer } from 'subscriptions-transport-ws';
import bodyParser from 'body-parser';
import { makeExecutableSchema } from 'graphql-tools';
import { MongoClient } from 'mongodb';
import cors from 'cors';
import passport from 'passport';
import typeDefs from '../schema';
import resolvers from '../resolvers';
import addModelsToContext from '../model';
import authenticate from './authenticate';
import { pubsub, subscriptionManager } from './subscriptions';
const schema = makeExecutableSchema({ typeDefs, resolvers });
const {
PORT = 3000,
WS_PORT = parseInt(PORT, 10) + 1,
MONGO_PORT = parseInt(PORT, 10) + 2,
MONGO_URL = `mongodb://localhost:${MONGO_PORT}/database`,
} = process.env;
async function startServer() {
const db = await MongoClient.connect(MONGO_URL);
const app = express().use('*', cors());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use((req, res, next) => {
req.context = addModelsToContext({ db, pubsub });
next();
});
authenticate(app);
app.use('/graphql', (req, res, next) => {
passport.authenticate('jwt', { session: false }, (err, user) => {
graphqlExpress(() => {
// Get the query, the same way express-graphql does it
// https://github.com/graphql/express-graphql/blob/3fa6e68582d6d933d37fa9e841da5d2aa39261cd/src/index.js#L257
const query = req.query.query || req.body.query;
if (query && query.length > 2000) {
// None of our app's queries are this long
// Probably indicates someone trying to send an overly expensive query
throw new Error('Query too large.');
}
return {
schema,
context: Object.assign({ user }, req.context),
debug: true,
formatError(e) {
console.log(e);
return e;
},
};
})(req, res, next);
})(req, res, next);
});
app.use('/graphiql', graphiqlExpress({
endpointURL: '/graphql',
}));
app.listen(PORT, () => console.log(
`API Server is now running on http://localhost:${PORT}`
));
// WebSocket server for subscriptions
const websocketServer = createServer((request, response) => {
response.writeHead(404);
response.end();
});
websocketServer.listen(WS_PORT, () => console.log(
`Websocket Server is now running on http://localhost:${WS_PORT}`
));
return new SubscriptionServer(
{
subscriptionManager,
// the onSubscribe function is called for every new subscription
// and we use it to set the GraphQL context for this subscription
onSubscribe: (msg, params) => {
return Object.assign({}, params, {
context: Object.assign({}, context),
});
},
},
websocketServer
);
}
startServer()
.then(() => {
console.log('All systems go');
})
.catch((e) => {
console.error('Uncaught error in startup');
console.error(e);
console.trace(e);
});