@graphql-mesh/json-schema
Version:
64 lines (61 loc) • 2.32 kB
JavaScript
import { PredefinedProxyOptions } from '@graphql-mesh/store';
import { createBundle, getGraphQLSchemaFromBundle } from '@omnigraph/json-schema';
import { readFileOrUrl } from '@graphql-mesh/utils';
import { getInterpolatedHeadersFactory } from '@graphql-mesh/string-interpolation';
import { process } from '@graphql-mesh/cross-helpers';
class JsonSchemaHandler {
constructor({ name, config, baseDir, store, pubsub, logger, importFn, fetchFn, }) {
this.name = name;
this.config = config;
this.baseDir = baseDir;
this.fetchFn = fetchFn;
this.importFn = importFn;
this.bundleStoreProxy = store.proxy('jsonSchemaBundle', PredefinedProxyOptions.JsonWithoutValidation);
this.pubsub = pubsub;
this.logger = logger;
}
async getDereferencedBundle() {
const config = this.config;
if ('bundlePath' in config) {
const headersFactory = getInterpolatedHeadersFactory(config.bundleHeaders);
const bundle = await readFileOrUrl(config.bundlePath, {
cwd: this.baseDir,
fetch: this.fetchFn,
logger: this.logger,
headers: headersFactory({
env: process.env,
}),
fallbackFormat: 'json',
importFn: this.importFn,
});
return bundle;
}
else {
return this.bundleStoreProxy.getWithSet(() => {
return 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 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,
};
}
}
export default JsonSchemaHandler;