-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
53 lines (43 loc) · 1.18 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
53
import Koa from 'koa'
import { execute, subscribe } from 'graphql'
import { createServer } from 'http'
import { SubscriptionServer } from 'subscriptions-transport-ws'
import Router from 'koa-router'
import { graphqlKoa, graphiqlKoa } from 'apollo-server-koa'
import koaBody from 'koa-bodyparser'
import schema from './services/graphql/schema/index'
import subscriptions from './subscriptions'
const port = 5000
const app = new Koa()
app.use(koaBody())
const router = new Router()
router.all('/graphql',
(ctx, next) => graphqlKoa({
schema,
rootValue: {
ctx,
},
})(ctx, next),
);
router.get('/graphiql', graphiqlKoa({
schema,
endpointURL: '/graphql',
subscriptionsEndpoint: `ws://localhost:${port}/subscriptions`
}));
app.use(router.routes()).use(router.allowedMethods())
if (!module.parent) {
subscriptions.init()
const ws = createServer(app.callback())
ws.listen(port, () => {
new SubscriptionServer({
execute,
subscribe,
schema
}, {
server: ws,
path: '/subscriptions',
})
})
console.log(`GraphQL server running on http://localhost:${port}`)
console.log(`GraphiQL client running on http://localhost:${port}/graphiql`)
}