-
-
Notifications
You must be signed in to change notification settings - Fork 262
/
index.js
142 lines (125 loc) · 3.07 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import 'dotenv/config';
import cors from 'cors';
import morgan from 'morgan';
import http from 'http';
import jwt from 'jsonwebtoken';
import DataLoader from 'dataloader';
import express from 'express';
import {
ApolloServer,
AuthenticationError,
} from 'apollo-server-express';
import schema from './schema';
import resolvers from './resolvers';
import models, { sequelize } from './models';
import loaders from './loaders';
const app = express();
app.use(cors());
app.use(morgan('dev'));
const getMe = async req => {
const token = req.headers['x-token'];
if (token) {
try {
return await jwt.verify(token, process.env.SECRET);
} catch (e) {
throw new AuthenticationError(
'Your session expired. Sign in again.',
);
}
}
};
const server = new ApolloServer({
introspection: true,
playground: true,
typeDefs: schema,
resolvers,
formatError: error => {
// remove the internal sequelize error message
// leave only the important validation error
const message = error.message
.replace('SequelizeValidationError: ', '')
.replace('Validation error: ', '');
return {
...error,
message,
};
},
context: async ({ req, connection }) => {
if (connection) {
return {
models,
loaders: {
user: new DataLoader(keys =>
loaders.user.batchUsers(keys, models),
),
},
};
}
if (req) {
const me = await getMe(req);
return {
models,
me,
secret: process.env.SECRET,
loaders: {
user: new DataLoader(keys =>
loaders.user.batchUsers(keys, models),
),
},
};
}
},
});
server.applyMiddleware({ app, path: '/graphql' });
const httpServer = http.createServer(app);
server.installSubscriptionHandlers(httpServer);
const isTest = !!process.env.TEST_DATABASE;
const isProduction = !!process.env.DATABASE_URL;
const port = process.env.PORT || 8000;
sequelize.sync({ force: isTest || isProduction }).then(async () => {
if (isTest || isProduction) {
createUsersWithMessages(new Date());
}
httpServer.listen({ port }, () => {
console.log(`Apollo Server on http://localhost:${port}/graphql`);
});
});
const createUsersWithMessages = async date => {
await models.User.create(
{
username: 'rwieruch',
email: 'hello@robin.com',
password: 'rwieruch',
role: 'ADMIN',
messages: [
{
text: 'Published the Road to learn React',
createdAt: date.setSeconds(date.getSeconds() + 1),
},
],
},
{
include: [models.Message],
},
);
await models.User.create(
{
username: 'ddavids',
email: 'hello@david.com',
password: 'ddavids',
messages: [
{
text: 'Happy to release ...',
createdAt: date.setSeconds(date.getSeconds() + 1),
},
{
text: 'Published a complete ...',
createdAt: date.setSeconds(date.getSeconds() + 1),
},
],
},
{
include: [models.Message],
},
);
};