@graphql-mesh/json-schema
Version:
73 lines (70 loc) • 2.91 kB
JavaScript
import { PredefinedProxyOptions } from '@graphql-mesh/store';
import { createBundle, getGraphQLSchemaFromBundle } from '@omnigraph/json-schema';
import { readFileOrUrl, loadFromModuleExportExpression } 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, }) {
this.name = name;
this.config = config;
this.baseDir = baseDir;
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,
operationHeaders: typeof config.operationHeaders === 'string' ? {} : config.operationHeaders,
});
});
}
}
async getMeshSource({ fetchFn }) {
this.fetchFn = fetchFn;
const bundle = await this.getDereferencedBundle();
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,
queryStringOptions: this.config.queryStringOptions,
queryParams: 'queryParams' in this.config ? this.config.queryParams : undefined,
});
return {
schema,
};
}
}
export default JsonSchemaHandler;