@graphql-mesh/serve-cli
Version:
143 lines (142 loc) • 5.51 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.addCommand = void 0;
exports.runProxy = runProxy;
const tslib_1 = require("tslib");
const node_cluster_1 = tslib_1.__importDefault(require("node:cluster"));
const serve_runtime_1 = require("@graphql-mesh/serve-runtime");
const utils_1 = require("@graphql-mesh/utils");
const cli_js_1 = require("../cli.js");
const config_js_1 = require("../config.js");
const server_js_1 = require("../server.js");
const handleFork_js_1 = require("./handleFork.js");
const addCommand = (ctx, cli) => cli
.command('proxy')
.description('serve a proxy to a GraphQL API and add additional features such as monitoring/tracing, caching, rate limiting, security, and more')
.argument('[endpoint]', 'URL of the endpoint GraphQL API to proxy')
.option('--schema <schemaPathOrUrl>', 'path to the GraphQL schema file or a url from where to pull the schema')
.action(async function proxy(endpoint) {
const { hiveCdnEndpoint, hiveCdnKey, hiveRegistryToken, maskedErrors, polling, hivePersistedDocumentsEndpoint, hivePersistedDocumentsToken, ...opts } = this.optsWithGlobals();
const loadedConfig = await (0, config_js_1.loadConfig)({
log: ctx.log,
configPath: opts.configPath,
quiet: !node_cluster_1.default.isPrimary,
configFileName: ctx.configFileName,
});
let proxy;
if (endpoint) {
proxy = { endpoint };
}
else if ('proxy' in loadedConfig) {
proxy = loadedConfig.proxy;
// TODO: how to provide hive-cdn-key?
}
if (!proxy) {
ctx.log.error('Proxy endpoint not defined. Please provide it in the [endpoint] argument or in the config file.');
process.exit(1);
}
let schema;
const hiveCdnEndpointOpt = opts.schema || hiveCdnEndpoint;
if (hiveCdnEndpointOpt) {
if (hiveCdnKey) {
if (!(0, utils_1.isUrl)(hiveCdnEndpointOpt)) {
ctx.log.error('Hive CDN endpoint must be a URL when providing --hive-cdn-key but got ' +
hiveCdnEndpointOpt);
process.exit(1);
}
schema = {
type: 'hive',
endpoint: hiveCdnEndpointOpt, // see validation above
key: hiveCdnKey,
};
}
else {
schema = opts.schema;
}
}
else if ('schema' in loadedConfig) {
schema = loadedConfig.schema;
// TODO: how to provide hive-cdn-key?
}
if (hiveCdnKey && !schema) {
process.stderr.write(`error: option '--schema <schemaPathOrUrl>' is required when providing '--hive-cdn-key <key>'\n`);
process.exit(1);
}
const pubsub = loadedConfig.pubsub || new utils_1.PubSub();
const cache = await (0, config_js_1.getCacheInstanceFromConfig)(loadedConfig, {
pubsub,
logger: ctx.log,
});
const builtinPlugins = await (0, config_js_1.getBuiltinPluginsFromConfig)({
...loadedConfig,
...opts,
}, {
cache,
logger: ctx.log,
});
const config = {
...cli_js_1.defaultOptions,
...loadedConfig,
...opts,
...(hiveRegistryToken
? {
reporting: {
...loadedConfig.reporting,
type: 'hive',
token: hiveRegistryToken,
},
}
: {}),
...(polling ? { pollingInterval: polling } : {}),
...(hivePersistedDocumentsEndpoint
? {
persistedDocuments: {
type: 'hive',
endpoint: hivePersistedDocumentsEndpoint ||
(loadedConfig.persistedDocuments &&
'endpoint' in loadedConfig.persistedDocuments &&
loadedConfig.persistedDocuments?.endpoint),
token: hivePersistedDocumentsToken ||
(loadedConfig.persistedDocuments &&
'token' in loadedConfig.persistedDocuments &&
loadedConfig.persistedDocuments?.token),
},
}
: {}),
proxy,
schema,
logging: loadedConfig.logging ?? ctx.log,
productName: ctx.productName,
productDescription: ctx.productDescription,
productPackageName: ctx.productPackageName,
productLogo: ctx.productLogo,
productLink: ctx.productLink,
pubsub,
cache,
plugins(ctx) {
const userPlugins = loadedConfig.plugins?.(ctx) ?? [];
return [...builtinPlugins, ...userPlugins];
},
};
if (maskedErrors != null) {
// overwrite masked errors from loaded config only when provided
config.maskedErrors = maskedErrors;
}
if (typeof config.pollingInterval === 'number' && config.pollingInterval < 10_000) {
process.stderr.write(`error: polling interval duration too short, use at least 10 seconds\n`);
process.exit(1);
}
return runProxy(ctx, config);
});
exports.addCommand = addCommand;
async function runProxy({ log }, config) {
if ((0, handleFork_js_1.handleFork)(log, config)) {
return;
}
log.info(`Proxying requests to ${config.proxy.endpoint}`);
const runtime = (0, serve_runtime_1.createGatewayRuntime)(config);
await (0, server_js_1.startServerForRuntime)(runtime, {
...config,
log,
});
}
;