import express from 'express' ; import http from 'http' ; import { ApolloServer as ApolloServerExpress , CorsOptions , ApolloServerExpressConfig , } from 'apollo-server-express' ; import type { AddressInfo } from 'net' ; import { format as urlFormat } from 'url' ; import { ApolloServerPluginDrainHttpServer } from 'apollo-server-core' ; export * from './exports' ; export interface ServerInfo { address : string ; family : string ; url : string ; port : number | string ; server : http . Server ; } export class ApolloServer extends ApolloServerExpress { private cors : CorsOptions | boolean | undefined ; private onHealthCheck : ( ( req : express . Request ) => Promise < any > ) | undefined ; private healthCheckPath : string | null | undefined ; private httpServer : http . Server ; constructor ( config ) { const httpServer = http . createServer ( ) ; super ( { ... config , plugins : [ ... ( config . plugins ?? [ ] ) , ApolloServerPluginDrainHttpServer ( { httpServer : httpServer , stopGracePeriodMillis : config . stopGracePeriodMillis , } ) , ] , } ) ; this . httpServer = httpServer ; this . cors = config . cors ; this . onHealthCheck = config . onHealthCheck ; this . healthCheckPath = config ?. healthCheckPath ; } private createServerInfo ( ) { const addressInfo = this . httpServer . address ( ) as AddressInfo ; let hostForUrl = addressInfo . address ; if ( hostForUrl === '' || hostForUrl === '::' ) { hostForUrl = 'localhost' ; } const url = urlFormat ( { protocol : 'http' , hostname : hostForUrl , port : addressInfo . port , pathname : this . graphqlPath , } ) ; return { ... addressInfo , server : this . httpServer , url , } ; } public override applyMiddleware ( ) { throw new Error ( 'To use Apollo Server with an existing express application, please use apollo-server-express' ) ; } public override async start ( ) { throw new Error ( "When using the `apollo-server` package, you don't need to call start(); just call listen()." ) ; } public async listen ( ... opts ) { await this . _start ( ) ; const app = express ( ) ; this . httpServer . on ( 'request' , app ) ; app . disable ( 'x-powered-by' ) ; super . applyMiddleware ( { app : app , path : '/' , bodyParserConfig : { limit : '50mb' } , onHealthCheck : this . onHealthCheck , cors : typeof this . cors !== 'undefined' ? this . cors : { origin : '*' , } , __internal_healthCheckPath : this . healthCheckPath , } ) ; await new Promise ( ( resolve ) => { this . httpServer . once ( 'listening' , resolve ) ; this . httpServer . listen ( ... ( opts . length ? opts : [ { port : 4000 } ] ) ) ; } ) ; return this . createServerInfo ( ) ; } }