@graphql-mesh/serve-cli
Version:
151 lines (150 loc) • 6.21 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.startServerForRuntime = startServerForRuntime;
exports.getGraphQLWSOptions = getGraphQLWSOptions;
const node_fs_1 = require("node:fs");
const node_http_1 = require("node:http");
const node_https_1 = require("node:https");
const utils_1 = require("@graphql-mesh/utils");
const cli_js_1 = require("./cli.js");
async function startServerForRuntime(runtime, { log, host = cli_js_1.defaultOptions.host, port = cli_js_1.defaultOptions.port, sslCredentials, maxHeaderSize = 16_384, disableWebsockets = false, }) {
const terminateStack = (0, utils_1.getTerminateStack)();
terminateStack.use(runtime);
process.on('message', message => {
if (message === 'invalidateUnifiedGraph') {
log.info(`Invalidating Supergraph`);
runtime.invalidateUnifiedGraph();
}
});
const serverOpts = {
log,
host,
port,
sslCredentials,
maxHeaderSize,
disableWebsockets,
};
const server = await startNodeHttpServer(runtime, serverOpts);
terminateStack.use(server);
return server;
}
async function startNodeHttpServer(gwRuntime, opts) {
const { log, host = cli_js_1.defaultOptions.host, port = cli_js_1.defaultOptions.port, sslCredentials, maxHeaderSize, disableWebsockets, } = opts;
let server;
let protocol;
if (sslCredentials) {
protocol = 'https';
const sslOptionsForNodeHttp = {};
if (sslCredentials.ca_file_name) {
sslOptionsForNodeHttp.ca = await node_fs_1.promises.readFile(sslCredentials.ca_file_name);
}
if (sslCredentials.cert_file_name) {
sslOptionsForNodeHttp.cert = await node_fs_1.promises.readFile(sslCredentials.cert_file_name);
}
if (sslCredentials.dh_params_file_name) {
sslOptionsForNodeHttp.dhparam = await node_fs_1.promises.readFile(sslCredentials.dh_params_file_name);
}
if (sslCredentials.key_file_name) {
sslOptionsForNodeHttp.key = await node_fs_1.promises.readFile(sslCredentials.key_file_name);
}
if (sslCredentials.passphrase) {
sslOptionsForNodeHttp.passphrase = sslCredentials.passphrase;
}
if (sslCredentials.ssl_ciphers) {
sslOptionsForNodeHttp.ciphers = sslCredentials.ssl_ciphers;
}
if (sslCredentials.ssl_prefer_low_memory_usage) {
sslOptionsForNodeHttp.honorCipherOrder = true;
}
server = (0, node_https_1.createServer)({
...sslOptionsForNodeHttp,
maxHeaderSize,
}, gwRuntime);
}
else {
protocol = 'http';
server = (0, node_http_1.createServer)({
maxHeaderSize,
}, gwRuntime);
}
const url = `${protocol}://${host}:${port}`.replace('0.0.0.0', 'localhost');
log.debug(`Starting server on ${url}`);
if (!disableWebsockets) {
const { WebSocketServer } = await Promise.resolve().then(() => __importStar(require('ws')));
const wsServer = new WebSocketServer({
path: gwRuntime.graphqlEndpoint,
server,
});
const graphqlWSOptions = getGraphQLWSOptions(gwRuntime);
const { useServer } = await Promise.resolve().then(() => __importStar(require('graphql-ws/lib/use/ws')));
useServer(graphqlWSOptions, wsServer);
}
return new Promise((resolve, reject) => {
server.once('error', reject);
server.listen(port, host, () => {
log.info(`Listening on ${url}`);
resolve((0, utils_1.createAsyncDisposable)(() => new Promise(resolve => {
process.stderr.write('\n');
log.info(`Stopping the server`);
server.closeAllConnections();
server.close(() => {
log.info(`Stopped the server successfully`);
resolve();
});
})));
});
});
}
function getGraphQLWSOptions(gwRuntime) {
return {
execute: (args) => args.rootValue.execute(args),
subscribe: (args) => args.rootValue.subscribe(args),
onSubscribe: async (ctx, msg) => {
const { schema, execute, subscribe, contextFactory, parse, validate } = gwRuntime.getEnveloped({
connectionParams: ctx.connectionParams,
req: ctx.extra.request,
});
const args = {
schema: schema || (await gwRuntime.getSchema()),
operationName: msg.payload.operationName,
document: parse(msg.payload.query),
variableValues: msg.payload.variables,
contextValue: await contextFactory(),
rootValue: {
execute,
subscribe,
},
};
if (args.schema) {
const errors = validate(args.schema, args.document);
if (errors.length)
return errors;
}
return args;
},
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
};
}
;