@graphql-mesh/new-openapi
Version:
52 lines (49 loc) • 2.08 kB
JavaScript
import { PredefinedProxyOptions } from '@graphql-mesh/store';
import { createBundle, getGraphQLSchemaFromBundle } from '@omnigraph/openapi';
class OpenAPIHandler {
constructor({ name, config, baseDir, fetchFn, store, pubsub, logger, }) {
this.name = name;
this.config = config;
this.baseDir = baseDir;
this.fetch = fetchFn;
this.bundleStoreProxy = store.proxy('jsonSchemaBundle', PredefinedProxyOptions.JsonWithoutValidation);
this.pubsub = pubsub;
this.logger = logger;
}
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.fetch,
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,
});
});
}
async getMeshSource() {
var _a, _b;
(_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 schema = await getGraphQLSchemaFromBundle(bundle, {
cwd: this.baseDir,
fetch: this.fetch,
pubsub: this.pubsub,
logger: this.logger,
baseUrl: this.config.baseUrl,
operationHeaders: this.config.operationHeaders,
});
return {
schema,
};
}
}
export default OpenAPIHandler;