index.ts 989 Bytes
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;