-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·92 lines (79 loc) · 2.24 KB
/
index.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
80
81
82
83
84
85
86
87
88
89
90
91
92
const Koa = require('koa');
const Router = require('koa-router');
const BodyParser = require('koa-bodyparser');
const logger = require('koa-logger');
const conditional = require('koa-conditional-get');
const etag = require('koa-etag');
const cors = require('@koa/cors');
const { createServer } = require('http');
const { graphqlKoa, graphiqlKoa } = require('apollo-server-koa');
const { execute, subscribe } = require('graphql');
const { SubscriptionServer } = require('subscriptions-transport-ws');
const schema = require('./src/schema');
const connectMongo = require('./src/connector/mongo-connector');
const { User, Link, Vote } = require('./src/models');
const formatError = require('./src/helper/fomatError');
const start = async () => {
const mongo = await connectMongo();
const app = new Koa();
const router = new Router();
app.use(cors());
app.use(conditional());
app.use(etag());
app.use(logger());
app.use(BodyParser());
router.post(
'/graphql',
graphqlKoa(async (req, res) => {
const { authorization } = req.headers;
const user = new User(mongo);
const currentUser = await user.authenticate(authorization);
return {
schema,
formatError,
context: {
user,
link: new Link(mongo),
vote: new Vote(mongo),
currentUser
}
};
})
);
router.get(
'/graphiql',
graphiqlKoa({
endpointURL: '/graphql',
passHeader: `'Authorization': 'bearer token-lucy@outlook.com'`,
subscriptionsEndpoint: 'ws://localhost:4000/subscriptions'
})
);
router.get('/', ctx => {
ctx.body = 'OK!!!!';
});
app.use(router.routes());
app.use(router.allowedMethods());
app.on('error', err => {
console.error('server error', err);
});
app.on('error', (err, ctx) => {
console.error('server error', err, ctx);
});
const server = createServer(app.callback());
server.listen(4000, () => {
SubscriptionServer.create(
{
execute,
subscribe,
schema
},
{
server,
path: '/subscriptions'
}
);
console.log('Server is running on http://localhost:4000');
console.log('GraphiQl is running on http://localhost:4000/graphiql');
});
};
start();