apollo-server-hapi: fix graphql gui request, bind server listen, and check for autoListen: false

This commit is contained in:
Evans Hauser 2018-05-10 22:12:16 -07:00
parent 98ca26a303
commit 6b80ac96a1
No known key found for this signature in database
GPG key ID: 88AF586817F52EEC

View file

@ -35,9 +35,10 @@ export const registerServer = async ({
if (request.path !== path) {
return h.continue;
}
if (!server.disableTools && request.method === 'get') {
//perform more expensive content-type check only if necessary
const accept = parseAll(request.app);
const accept = parseAll(request.headers);
const types = accept.mediaTypes as string[];
const prefersHTML =
types.find(
@ -50,10 +51,11 @@ export const registerServer = async ({
renderPlaygroundPage({
subscriptionsEndpoint: server.subscriptions && path,
endpoint: path,
version: '',
version: '1.4.0',
}),
)
.type('text/html');
.type('text/html')
.takeover();
}
}
return h.continue;
@ -73,12 +75,28 @@ export const registerServer = async ({
server.use({ path, getHttp: () => app.listener });
const listen = server.listen;
const listen = server.listen.bind(server);
server.listen = async options => {
//requires that autoListen is false, so that
//hapi sets up app.listener without start
await app.start();
//While this is not strictly necessary, it ensures that apollo server calls
//listen first, setting the port. Otherwise the hapi server constructor
//sets the port
if (app.listener.listening) {
throw Error(
`
Ensure that constructor of Hapi server sets autoListen to false, as follows:
const app = Hapi.server({
autoListen: false,
//other parameters
});
`,
);
}
//starts the hapi listener at a random port when engine proxy used,
//otherwise will start the server at the provided port
return listen({ ...options });