import 'reflect-metadata' ; import { ApolloServer } from 'apollo-server-express' ; import cors from 'cors' ; import express , { Express , Response } from 'express' ; import { buildSchema } from 'type-graphql' ; import { GQLCtx , Request } from 'src/common-types/gql' ; import { resolvers } from 'src/controllers' ; import { userMiddleware , handleAuthenticationError , } from 'src/controllers/Auth/middleware' ; const PORT = process . env . PORT || 5000 ; export const main = async ( app ) => { app . use ( cors ( { credentials : true , origin : true } ) ) ; app . use ( userMiddleware ) ; app . use ( handleAuthenticationError ) ; const schema = await buildSchema ( { resolvers } ) ; const server = new ApolloServer ( { schema , context : ( { req , res } ) : GQLCtx => ( { req , res , user : req . user , } ) , } ) ; await server . start ( ) ; server . applyMiddleware ( { app , cors : false , path : '/graphql' } ) ; } ; if ( require . main === module ) { ( async ( ) => { const app = express ( ) ; await main ( app ) ; app . listen ( PORT , ( ) => console . log ( `Listening on http://localhost:${ PORT }/graphql` ) ) ; } ) ( ) ; }