Skip to content

Commit

Permalink
Merge pull request #1080 from apollographql/server-2.0/http-option
Browse files Browse the repository at this point in the history
apollo-server-core: move http options under own key in listenOptions
  • Loading branch information
evans authored May 22, 2018
2 parents c9eaae6 + 57328e2 commit dccb482
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 41 deletions.
6 changes: 4 additions & 2 deletions packages/apollo-server-core/src/ApolloServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
72 changes: 38 additions & 34 deletions packages/apollo-server-core/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
},
Expand All @@ -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';

listeningAddress.url = require('url').format({
protocol: 'http',
hostname: hostForUrl,
port: listeningAddress.port,
pathname: this.graphqlPath,
});
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,
});

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));
}
});
}

Expand Down
6 changes: 1 addition & 5 deletions packages/apollo-server-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
Expand Down

0 comments on commit dccb482

Please sign in to comment.