@graphql-mesh/openapi
Version:
62 lines (59 loc) • 2.67 kB
JavaScript
import { PredefinedProxyOptions } from '@graphql-mesh/store';
import { loadFromModuleExportExpression } from '@graphql-mesh/utils';
import { createBundle, getGraphQLSchemaFromBundle } from '@omnigraph/openapi';
class OpenAPIHandler {
constructor({ name, config, baseDir, store, pubsub, logger, importFn, }) {
this.name = name;
this.config = config;
this.baseDir = baseDir;
this.bundleStoreProxy = store.proxy('jsonSchemaBundle', PredefinedProxyOptions.JsonWithoutValidation);
this.pubsub = pubsub;
this.logger = logger;
this.importFn = importFn;
}
async getDereferencedBundle() {
return this.bundleStoreProxy.getWithSet(() => {
var _a, _b;
(_a = this.logger) === null || _a === void 0 ? void 0 : _a.debug('Creating the bundle');
return createBundle(this.name, {
...this.config,
cwd: this.baseDir,
fetch: this.fetchFn,
logger: this.logger,
ignoreErrorResponses: this.config.ignoreErrorResponses,
selectQueryOrMutationField: (_b = this.config.selectQueryOrMutationField) === null || _b === void 0 ? void 0 : _b.map(({ type, fieldName }) => ({
type: type.toLowerCase(),
fieldName,
})),
fallbackFormat: this.config.fallbackFormat,
operationHeaders: typeof this.config.operationHeaders === 'string' ? {} : this.config.operationHeaders,
});
});
}
async getMeshSource({ fetchFn }) {
var _a, _b;
this.fetchFn = fetchFn;
(_a = this.logger) === null || _a === void 0 ? void 0 : _a.debug('Getting the bundle');
const bundle = await this.getDereferencedBundle();
(_b = this.logger) === null || _b === void 0 ? void 0 : _b.debug('Generating GraphQL Schema from bundle');
const operationHeadersConfig = typeof this.config.operationHeaders === 'string'
? await loadFromModuleExportExpression(this.config.operationHeaders, {
cwd: this.baseDir,
importFn: this.importFn,
defaultExportName: 'default',
})
: this.config.operationHeaders;
const schema = await getGraphQLSchemaFromBundle(bundle, {
cwd: this.baseDir,
fetch: this.fetchFn,
pubsub: this.pubsub,
logger: this.logger,
baseUrl: this.config.baseUrl,
operationHeaders: operationHeadersConfig,
});
return {
schema,
};
}
}
export default OpenAPIHandler;