This repository was archived by the owner on Jun 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
62 lines (52 loc) · 1.27 KB
/
app.js
File metadata and controls
62 lines (52 loc) · 1.27 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
import express from 'express';
import bodyParser from 'body-parser';
import cors from 'cors';
import jwt from 'jsonwebtoken';
import { createServer } from 'http';
import { execute, subscribe } from 'graphql';
import { SubscriptionServer } from 'subscriptions-transport-ws';
import { graphiqlExpress, graphqlExpress } from 'apollo-server-express';
import schema from './src/schema';
import models from './src/models/db';
require('dotenv').config();
const { SECRET } = process.env;
const app = express();
const addUser = (req) => {
const token = req.headers.authorization;
try {
const { user } = jwt.verify(token, SECRET);
req.user = user;
} catch (error) {
console.error(error);
}
req.next();
};
app.use(cors('*'));
app.use(addUser);
app.use(
'/graphiql',
graphiqlExpress({
endpointURL: '/graphql',
subscriptionsEndpoint: 'ws://localhost:3700/subscriptions',
})
);
app.use(
'/graphql',
bodyParser.json(),
graphqlExpress(req => ({
schema,
context: { models, SECRET, user: req.user }
}))
);
const server = createServer(app);
models.sequelize.sync().then(() =>
server.listen(3700, () => {
new SubscriptionServer(
{
execute,
subscribe,
schema
},
{ server, path: './subscriptions' }
);
}));