@graphql-mesh/cli
Version:
50 lines (49 loc) • 1.76 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.startNodeHttpServer = startNodeHttpServer;
/* eslint-disable import/no-nodejs-modules */
const fs_1 = require("fs");
const getGraphQLWSOpts_js_1 = require("./getGraphQLWSOpts.js");
async function startNodeHttpServer({ meshHTTPHandler, getBuiltMesh, sslCredentials, graphqlPath, hostname, port, }) {
let server;
if (sslCredentials) {
const [key, cert] = await Promise.all([
fs_1.promises.readFile(sslCredentials.key),
fs_1.promises.readFile(sslCredentials.cert),
]);
const nodeHttps = await import('https');
server = nodeHttps.createServer({
key,
cert,
}, meshHTTPHandler);
}
else {
const nodeHttp = await import('http');
server = nodeHttp.createServer(meshHTTPHandler);
}
const ws = await import('ws');
const wsServer = new ws.WebSocketServer({
path: graphqlPath,
server,
});
const { useServer } = await import('graphql-ws/use/ws');
useServer((0, getGraphQLWSOpts_js_1.getGraphQLWSOptions)(getBuiltMesh), wsServer);
return new Promise((resolve, reject) => {
server.once('error', err => reject(err));
server.listen(port, hostname, () => {
resolve({
stop: () => new Promise((resolve, reject) => {
server.closeAllConnections();
server.close(err => {
if (err) {
reject(err);
}
else {
resolve();
}
});
}),
});
});
});
}
;