apollo-server-core: move http options under own key in listenOptions

This commit is contained in:
Evans Hauser 2018-05-21 23:20:56 -07:00
parent c9eaae6567
commit 57328e2214
No known key found for this signature in database
GPG key ID: 88AF586817F52EEC
3 changed files with 42 additions and 40 deletions

View file

@ -396,9 +396,11 @@ describe('ApolloServerBase', () => {
level: 'ERROR',
},
},
port: 4000,
http: {
port: 4242,
},
});
expect(enginePort).to.equal(4000);
expect(enginePort).to.equal(4242);
//Check engine responding
const engineApolloFetch = createApolloFetch({ uri: engineUri });

View file

@ -127,8 +127,11 @@ export class ApolloServerBase<Request = RequestInit> {
this.http = this.getHttp();
const options = {
port: process.env.PORT || 4000,
...opts,
http: {
port: process.env.PORT || 4000,
...opts.http,
},
};
if (opts.subscriptions !== false) {
@ -160,7 +163,7 @@ export class ApolloServerBase<Request = RequestInit> {
this.engineProxy.listen(
{
graphqlPaths: [this.graphqlPath],
port: options.port,
port: options.http.port,
httpServer: this.http,
launcherOptions: options.engineLauncherOptions,
},
@ -180,39 +183,40 @@ export class ApolloServerBase<Request = RequestInit> {
// https://nodejs.org/api/net.html#net_server_listen_options_callback
// https://github.com/apollographql/apollo-server/pull/979/files/33ea0c92a1e4e76c8915ff08806f15dae391e1f0#discussion_r184470435
// https://github.com/apollographql/apollo-server/pull/979#discussion_r184471445
this.http.listen(
{
port: options.port,
host: options.host,
path: options.path,
backlog: options.backlog,
exclusive: options.exclusive,
},
() => {
const listeningAddress: any = this.http.address();
// Convert IPs which mean "any address" (IPv4 or IPv6) into localhost
// corresponding loopback ip. Note that the url field we're setting is
// primarily for consumption by our test suite. If this heuristic is
// wrong for your use case, explicitly specify a frontend host (in the
// `frontends.host` field in your engine config, or in the `host`
// option to ApolloServer.listen).
let hostForUrl = listeningAddress.address;
if (
listeningAddress.address === '' ||
listeningAddress.address === '::'
)
hostForUrl = 'localhost';
function listenCallback() {
const listeningAddress: any = this.http.address();
// Convert IPs which mean "any address" (IPv4 or IPv6) into localhost
// corresponding loopback ip. Note that the url field we're setting is
// primarily for consumption by our test suite. If this heuristic is
// wrong for your use case, explicitly specify a frontend host (in the
// `frontends.host` field in your engine config, or in the `host`
// option to ApolloServer.listen).
let hostForUrl = listeningAddress.address;
if (
listeningAddress.address === '' ||
listeningAddress.address === '::'
)
hostForUrl = 'localhost';
listeningAddress.url = require('url').format({
protocol: 'http',
hostname: hostForUrl,
port: listeningAddress.port,
pathname: this.graphqlPath,
});
listeningAddress.url = require('url').format({
protocol: 'http',
hostname: hostForUrl,
port: listeningAddress.port,
pathname: this.graphqlPath,
});
resolve(listeningAddress);
},
);
resolve(listeningAddress);
}
if (options.http.handle) {
this.http.listen(
options.http.handle,
options.http.backlog,
listenCallback.bind(this),
);
} else {
this.http.listen(options.http, listenCallback.bind(this));
}
});
}

View file

@ -59,11 +59,7 @@ export interface ListenOptions {
// node http listen options
// https://nodejs.org/api/net.html#net_server_listen_options_callback
// https://github.com/apollographql/apollo-server/pull/979#discussion_r184483094
port?: string | number;
host?: string;
path?: string;
backlog?: number;
exclusive?: boolean;
http?: HttpListenOptions | any | { handle: any; backlog?: number };
// XXX clean this up
engineInRequestPath?: boolean;
engineProxy?: boolean | Record<string, any>;