UNPKG

@graphql-mesh/utils

Version:
24 lines (23 loc) 1.14 kB
/* eslint-disable @typescript-eslint/return-await */ import { path } from '@graphql-mesh/cross-helpers'; import { fakePromise } from '@graphql-tools/utils'; import { handleMaybePromise } from '@whatwg-node/promise-helpers'; import { defaultImportFn } from './defaultImportFn.js'; export function loadFromModuleExportExpression(expression, options) { if (typeof expression !== 'string') { return fakePromise(expression); } const { defaultExportName, cwd, importFn = defaultImportFn } = options || {}; const [modulePath, exportName = defaultExportName] = expression.split('#'); return handleMaybePromise(() => tryImport(modulePath, cwd, importFn), mod => mod[exportName] || (mod.default && mod.default[exportName]) || mod.default || mod); } function tryImport(modulePath, cwd, importFn) { return handleMaybePromise(() => importFn(modulePath), m => m, () => { if (!path.isAbsolute(modulePath)) { const absoluteModulePath = path.isAbsolute(modulePath) ? modulePath : path.join(cwd, modulePath); return importFn(absoluteModulePath); } }); }