@graphql-mesh/soap
Version:
67 lines (66 loc) • 2.85 kB
JavaScript
import { process } from '@graphql-mesh/cross-helpers';
import { PredefinedProxyOptions } from '@graphql-mesh/store';
import { getInterpolatedHeadersFactory } from '@graphql-mesh/string-interpolation';
import { readFileOrUrl } from '@graphql-mesh/utils';
import { createExecutorFromSchemaAST, SOAPLoader } from '@omnigraph/soap';
export default class SoapHandler {
constructor({ name, config, store, baseDir, importFn, logger, }) {
this.name = name;
this.config = config;
this.soapSDLProxy = store.proxy('schemaWithAnnotations', PredefinedProxyOptions.GraphQLSchemaWithDiffing);
this.baseDir = baseDir;
this.importFn = importFn;
this.logger = logger;
}
async getMeshSource({ fetchFn }) {
let schema;
const schemaHeadersFactory = getInterpolatedHeadersFactory(this.config.schemaHeaders);
if (this.config.source.endsWith('.graphql')) {
schema = await readFileOrUrl(this.config.source, {
allowUnknownExtensions: true,
cwd: this.baseDir,
fetch: fetchFn,
importFn: this.importFn,
logger: this.logger,
headers: schemaHeadersFactory({ env: process.env }),
});
}
else {
schema = await this.soapSDLProxy.getWithSet(async () => {
const soapLoader = new SOAPLoader({
subgraphName: this.name,
fetch: fetchFn,
logger: this.logger,
schemaHeaders: this.config.schemaHeaders,
operationHeaders: this.config.operationHeaders,
soapHeaders: this.config.soapHeaders,
bodyAlias: this.config.bodyAlias,
});
const wsdlLocation = this.config.source;
const wsdl = await readFileOrUrl(wsdlLocation, {
allowUnknownExtensions: true,
cwd: this.baseDir,
fetch: fetchFn,
importFn: this.importFn,
logger: this.logger,
headers: schemaHeadersFactory({ env: process.env }),
});
const object = await soapLoader.loadWSDL(wsdl);
soapLoader.loadedLocations.set(wsdlLocation, object);
return soapLoader.buildSchema();
});
}
// Create executor lazily for faster startup
let executor;
const operationHeaders = this.config.operationHeaders;
return {
schema,
executor(...args) {
if (!executor) {
executor = createExecutorFromSchemaAST(schema, fetchFn, operationHeaders);
}
return executor(...args);
},
};
}
}