import * as connectRedis from "connect-redis"; import * as cors from "cors"; import * as dotenv from "dotenv"; import * as Express from "express"; import * as session from "express-session"; import { GraphQLSchema } from "graphql"; import "reflect-metadata"; import { formatArgumentValidationError } from "type-graphql"; import { createConnection } from "typeorm"; import { ApolloServerWithQueryComplexity } from "./ApolloServerWithQueryComplexity"; import { redis } from "./redis"; import { IMyContext } from "./types/MyContext"; import { createSchema } from "./utils/createSchema"; dotenv.config(); const main = async () => { try { await createConnection(); } catch (err) { throw new Error(err); } let schema: GraphQLSchema; try { schema = await createSchema(); } catch (err) { throw new Error(err); } const apolloServer = new ApolloServerWithQueryComplexity({ schema, formatError: formatArgumentValidationError, context: ({ req, res }: IMyContext) => ({ req, res }) }); const app = Express(); const RedisStore = connectRedis(session); app.use( cors({ credentials: true, origin: "http://localhost:3000" }) ); app.use( session({ store: new RedisStore({ client: redis as any }), name: "qid", secret: process.env.SESSION_SECRET!, resave: false, saveUninitialized: false, cookie: { httpOnly: true, secure: process.env.NODE_ENV === "production", maxAge: 1000 * 60 * 60 * 24 * 7 * 365 // 7 years } }) ); apolloServer.applyMiddleware({ app, cors: false }); app.listen(4000, () => { console.log("Server started on http://localhost:4000/graphql"); }); }; main().catch(err => { console.error(err); });