index.ts
989 Bytes
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
import * as express from "express";
import { graphqlExpress } from "apollo-server-express";
import * as R from "ramda";
import * as jwt from "jsonwebtoken";
import * as bodyParser from "body-parser";
import schema from "./schema";
import { Connection } from "typeorm";
const { apolloUploadExpress } = require("apollo-upload-server");
const router = express.Router();
const formatError = R.always("GraphQL Error"); // OFF NOW
const extractSession = req =>
req.headers["authorization"]
? req.headers["authorization"].split(" ")[1]
: null;
export interface Context {
connection: Connection;
session: {
id: string;
};
}
router.use(
"/",
bodyParser.json(),
apolloUploadExpress(),
graphqlExpress(async (req, res) => {
const connection = req!.app.locals.connection;
const session = { id: extractSession(req) };
console.log(session);
const context: Context = { connection, session };
return { schema, context };
})
);
export default router;