UNPKG

@graphql-mesh/serve-cli

Version:
157 lines (156 loc) • 7.01 kB
"use strict"; 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.defaultConfigFileName = exports.defaultConfigExtensions = void 0; exports.createDefaultConfigPaths = createDefaultConfigPaths; exports.loadConfig = loadConfig; exports.getBuiltinPluginsFromConfig = getBuiltinPluginsFromConfig; exports.getCacheInstanceFromConfig = getCacheInstanceFromConfig; /* eslint-disable @typescript-eslint/no-unused-expressions */ const promises_1 = require("node:fs/promises"); const node_path_1 = require("node:path"); const node_url_1 = require("node:url"); exports.defaultConfigExtensions = ['.ts', '.mts', '.cts', '.js', '.mjs', '.cjs']; exports.defaultConfigFileName = 'gateway.config'; function createDefaultConfigPaths(configFileName) { return exports.defaultConfigExtensions.map(ext => `${configFileName}${ext}`); } async function loadConfig(opts) { let importedConfig = null; if (!opts.configPath) { !opts.quiet && opts.log.debug(`Searching for default config files`); const configPaths = [ ...createDefaultConfigPaths(exports.defaultConfigFileName), ...createDefaultConfigPaths(opts.configFileName), ]; for (const configPath of configPaths) { const absoluteConfigPath = (0, node_path_1.join)(process.cwd(), configPath); const exists = await (0, promises_1.lstat)(absoluteConfigPath) .then(() => true) .catch(() => false); if (exists) { !opts.quiet && opts.log.info(`Found default config file ${absoluteConfigPath}`); const importUrl = (0, node_url_1.pathToFileURL)(absoluteConfigPath).toString(); const module = await Promise.resolve(`${importUrl}`).then(s => __importStar(require(s))); importedConfig = Object(module).gatewayConfig || null; if (!importedConfig) { !opts.quiet && opts.log.warn(`No "gatewayConfig" exported from config file at ${absoluteConfigPath}`); } break; } } } else { // using user-provided config const configPath = (0, node_path_1.isAbsolute)(opts.configPath) ? opts.configPath : (0, node_path_1.join)(process.cwd(), opts.configPath); !opts.quiet && opts.log.info(`Loading config file at path ${configPath}`); const exists = await (0, promises_1.lstat)(configPath) .then(() => true) .catch(() => false); if (!exists) { throw new Error(`Cannot find config file at ${configPath}`); } const importUrl = (0, node_url_1.pathToFileURL)(configPath).toString(); const module = await Promise.resolve(`${importUrl}`).then(s => __importStar(require(s))); importedConfig = Object(module).gatewayConfig || null; if (!importedConfig) { throw new Error(`No "gatewayConfig" exported from config file at ${configPath}`); } } if (importedConfig) { !opts.quiet && opts.log.info('Loaded config'); } else { !opts.quiet && opts.log.debug('No config loaded'); } // TODO: validate imported config return importedConfig || {}; } async function getBuiltinPluginsFromConfig(config, ctx) { const plugins = []; if (config.jwt) { const { useJWT } = await Promise.resolve().then(() => __importStar(require('@graphql-mesh/plugin-jwt-auth'))); plugins.push(useJWT(config.jwt)); } if (config.prometheus) { const { default: useMeshPrometheus } = await Promise.resolve().then(() => __importStar(require('@graphql-mesh/plugin-prometheus'))); plugins.push(useMeshPrometheus(config.prometheus)); } if (config.openTelemetry) { const { useOpenTelemetry } = await Promise.resolve().then(() => __importStar(require('@graphql-mesh/plugin-opentelemetry'))); plugins.push(useOpenTelemetry({ logger: ctx.logger, ...config.openTelemetry, })); } if (config.rateLimiting) { const { default: useMeshRateLimit } = await Promise.resolve().then(() => __importStar(require('@graphql-mesh/plugin-rate-limit'))); plugins.push(useMeshRateLimit({ cache: ctx.cache, ...config.rateLimiting, })); } if (config.jit) { const { useJIT } = await Promise.resolve().then(() => __importStar(require('@graphql-mesh/plugin-jit'))); plugins.push(useJIT()); } return plugins; } async function getCacheInstanceFromConfig(config, ctx) { if (config.cache && 'type' in config.cache) { switch (config.cache.type) { case 'redis': { const { default: RedisCache } = await Promise.resolve().then(() => __importStar(require('@graphql-mesh/cache-redis'))); return new RedisCache({ ...ctx, ...config.cache, }); } case 'cfw-kv': { const { default: CloudflareKVCacheStorage } = await Promise.resolve().then(() => __importStar(require('@graphql-mesh/cache-cfw-kv'))); return new CloudflareKVCacheStorage({ ...ctx, ...config.cache, }); } } if (config.cache.type !== 'localforage') { ctx.logger.warn('Unknown cache type, falling back to localforage', config.cache); } const { default: LocalforageCache } = await Promise.resolve().then(() => __importStar(require('@graphql-mesh/cache-localforage'))); return new LocalforageCache({ ...ctx, ...config.cache, }); } if (config.cache) { return config.cache; } const { default: LocalforageCache } = await Promise.resolve().then(() => __importStar(require('@graphql-mesh/cache-localforage'))); return new LocalforageCache(); }