UNPKG

@graphql-mesh/serve-cli

Version:
127 lines (126 loc) 5.01 kB
/* eslint-disable @typescript-eslint/no-unused-expressions */ import { lstat } from 'node:fs/promises'; import { isAbsolute, join } from 'node:path'; import { pathToFileURL } from 'node:url'; export const defaultConfigExtensions = ['.ts', '.mts', '.cts', '.js', '.mjs', '.cjs']; export const defaultConfigFileName = 'gateway.config'; export function createDefaultConfigPaths(configFileName) { return defaultConfigExtensions.map(ext => `${configFileName}${ext}`); } export async function loadConfig(opts) { let importedConfig = null; if (!opts.configPath) { !opts.quiet && opts.log.debug(`Searching for default config files`); const configPaths = [ ...createDefaultConfigPaths(defaultConfigFileName), ...createDefaultConfigPaths(opts.configFileName), ]; for (const configPath of configPaths) { const absoluteConfigPath = join(process.cwd(), configPath); const exists = await lstat(absoluteConfigPath) .then(() => true) .catch(() => false); if (exists) { !opts.quiet && opts.log.info(`Found default config file ${absoluteConfigPath}`); const importUrl = pathToFileURL(absoluteConfigPath).toString(); const module = await import(importUrl); 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 = isAbsolute(opts.configPath) ? opts.configPath : join(process.cwd(), opts.configPath); !opts.quiet && opts.log.info(`Loading config file at path ${configPath}`); const exists = await lstat(configPath) .then(() => true) .catch(() => false); if (!exists) { throw new Error(`Cannot find config file at ${configPath}`); } const importUrl = pathToFileURL(configPath).toString(); const module = await import(importUrl); 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 || {}; } export async function getBuiltinPluginsFromConfig(config, ctx) { const plugins = []; if (config.jwt) { const { useJWT } = await import('@graphql-mesh/plugin-jwt-auth'); plugins.push(useJWT(config.jwt)); } if (config.prometheus) { const { default: useMeshPrometheus } = await import('@graphql-mesh/plugin-prometheus'); plugins.push(useMeshPrometheus(config.prometheus)); } if (config.openTelemetry) { const { useOpenTelemetry } = await import('@graphql-mesh/plugin-opentelemetry'); plugins.push(useOpenTelemetry({ logger: ctx.logger, ...config.openTelemetry, })); } if (config.rateLimiting) { const { default: useMeshRateLimit } = await import('@graphql-mesh/plugin-rate-limit'); plugins.push(useMeshRateLimit({ cache: ctx.cache, ...config.rateLimiting, })); } if (config.jit) { const { useJIT } = await import('@graphql-mesh/plugin-jit'); plugins.push(useJIT()); } return plugins; } export async function getCacheInstanceFromConfig(config, ctx) { if (config.cache && 'type' in config.cache) { switch (config.cache.type) { case 'redis': { const { default: RedisCache } = await import('@graphql-mesh/cache-redis'); return new RedisCache({ ...ctx, ...config.cache, }); } case 'cfw-kv': { const { default: CloudflareKVCacheStorage } = await import('@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 import('@graphql-mesh/cache-localforage'); return new LocalforageCache({ ...ctx, ...config.cache, }); } if (config.cache) { return config.cache; } const { default: LocalforageCache } = await import('@graphql-mesh/cache-localforage'); return new LocalforageCache(); }