-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathkoa2-server.js
79 lines (57 loc) · 1.94 KB
/
koa2-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
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
// See http://dev.apollodata.com/tools/apollo-server/setup.html#apolloKoa
import koa from 'koa';
import koaRouter from 'koa-router';
import koaBody from 'koa-bodyparser';
import { apolloKoa, graphiqlKoa } from 'apollo-server';
import cors from 'kcors';
import { createServer } from 'http';
import { SubscriptionServer } from 'subscriptions-transport-ws';
import { printSchema } from 'graphql/utilities/schemaPrinter';
import { subscriptionManager } from './data/subscriptions';
import schema from './data/schema';
const app = new koa();
const router = new koaRouter();
const GRAPHQL_PORT = 8080;
const WS_PORT = 8090;
const PORT = 3000;
app.use(koaBody());
app.use(cors());
import { forbidUndefinedInResolve } from 'graphql-tools';
forbidUndefinedInResolve(schema);
// logging
import { addErrorLoggingToSchema } from 'graphql-tools';
const logger = { log: (e) => console.error(e.stack) };
addErrorLoggingToSchema(schema, logger);
// Setup Apollo server
// For more options
// http://dev.apollodata.com/tools/apollo-server/setup.html
const apolloServer = apolloKoa({
schema,
context: {}
})
router.post('/graphql', apolloServer);
router.get('/schema', (ctx) => {
this.type = 'text/plain';
this.body = printSchema(schema);
});
// Add suppport for GraphiQL in-browser IDE exploration
router.get('/graphiql', graphiqlKoa({ endpointURL: '/graphql' }));
app.use(router.routes());
app.use(router.allowedMethods());
// app.listen(PORT);
app.listen(GRAPHQL_PORT, () => console.log(
`GraphQL Server is now running on http://localhost:${GRAPHQL_PORT}/graphql`
));
// WebSocket server for subscriptions
const websocketServer = createServer((request, response) => {
response.writeHead(404);
response.end();
});
websocketServer.listen(WS_PORT, () => console.log( // eslint-disable-line no-console
`Websocket Server is now running on http://localhost:${WS_PORT}`
));
// eslint-disable-next-line
new SubscriptionServer(
{ subscriptionManager },
websocketServer
);