-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
47 lines (39 loc) · 960 Bytes
/
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
import express from 'express';
import { json } from 'body-parser';
import cookieParser from 'cookie-parser';
import { graphqlExpress } from 'graphql-server-express';
import { formatError } from 'apollo-errors';
import { createExpressContext } from 'apollo-resolvers';
import { GraphQLError } from 'graphql';
import { UserModel } from './models';
import schema from './schema';
const app = express();
const auth = async (req, res, next) => {
const User = new UserModel(null);
if (req.cookies.token) {
req.user = await User.authenticate(req.cookies.token);
} else {
req.user = null;
}
next();
};
app.use('/graphql',
cookieParser(),
json(),
auth,
graphqlExpress((req, res) => {
const { user } = req;
const models = {
User: new UserModel(user)
};
const context = createExpressContext({
models,
user
}, res);
return {
schema,
formatError,
context
};
})
)