@graphql-mesh/json-schema
Version:
66 lines (62 loc) • 2.36 kB
JavaScript
'use strict';
const store = require('@graphql-mesh/store');
const jsonSchema = require('@omnigraph/json-schema');
const utils = require('@graphql-mesh/utils');
const stringInterpolation = require('@graphql-mesh/string-interpolation');
const crossHelpers = require('@graphql-mesh/cross-helpers');
class JsonSchemaHandler {
constructor({ name, config, baseDir, store: store$1, pubsub, logger, importFn, fetchFn, }) {
this.name = name;
this.config = config;
this.baseDir = baseDir;
this.fetchFn = fetchFn;
this.importFn = importFn;
this.bundleStoreProxy = store$1.proxy('jsonSchemaBundle', store.PredefinedProxyOptions.JsonWithoutValidation);
this.pubsub = pubsub;
this.logger = logger;
}
async getDereferencedBundle() {
const config = this.config;
if ('bundlePath' in config) {
const headersFactory = stringInterpolation.getInterpolatedHeadersFactory(config.bundleHeaders);
const bundle = await utils.readFileOrUrl(config.bundlePath, {
cwd: this.baseDir,
fetch: this.fetchFn,
logger: this.logger,
headers: headersFactory({
env: crossHelpers.process.env,
}),
fallbackFormat: 'json',
importFn: this.importFn,
});
return bundle;
}
else {
return this.bundleStoreProxy.getWithSet(() => {
return jsonSchema.createBundle(this.name, {
...config,
operations: config.operations,
cwd: this.baseDir,
fetch: this.fetchFn,
logger: this.logger,
});
});
}
}
async getMeshSource() {
const bundle = await this.getDereferencedBundle();
const schema = await jsonSchema.getGraphQLSchemaFromBundle(bundle, {
cwd: this.baseDir,
fetch: this.fetchFn,
pubsub: this.pubsub,
logger: this.logger,
baseUrl: this.config.baseUrl,
operationHeaders: this.config.operationHeaders,
queryStringOptions: this.config.queryStringOptions,
});
return {
schema,
};
}
}
module.exports = JsonSchemaHandler;