@graphql-mesh/grpc
Version:
49 lines (48 loc) • 2.23 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const graphql_1 = require("graphql");
const store_1 = require("@graphql-mesh/store");
const transport_grpc_1 = require("@graphql-mesh/transport-grpc");
const utils_1 = require("@graphql-mesh/utils");
const grpc_1 = require("@omnigraph/grpc");
class GrpcHandler {
constructor({ name, config, baseDir, store, logger, pubsub, importFn, }) {
this.logger = logger;
this.config = config;
this.baseDir = baseDir;
this.schemaWithAnnotationsProxy = store.proxy('schemaWithAnnotations', store_1.PredefinedProxyOptions.GraphQLSchemaWithDiffing);
this.pubsub = pubsub;
this.importFn = importFn;
this.sourceName = name;
}
getCachedNonExecutableSchema() {
const interpolatedSource = this.config.source?.toString();
if (interpolatedSource?.endsWith('.graphql')) {
this.logger.info(`Fetching GraphQL Schema with annotations`);
return (0, utils_1.readFileOrUrl)(interpolatedSource, {
allowUnknownExtensions: true,
cwd: this.baseDir,
fetch: this.fetchFn,
importFn: this.importFn,
logger: this.logger,
headers: this.config.schemaHeaders,
}).then(sdl => (0, graphql_1.buildSchema)(sdl, { assumeValidSDL: true, assumeValid: true }));
}
return this.schemaWithAnnotationsProxy.getWithSet(() => (0, grpc_1.loadGrpcSubgraph)(this.sourceName, this.config)({ cwd: this.baseDir, logger: this.logger })
.schema$);
}
getMeshSource() {
const transport = new transport_grpc_1.GrpcTransportHelper(this.sourceName, this.baseDir, this.logger, this.config.endpoint, this.config);
const subId = this.pubsub.subscribe('destroy', () => {
transport.dispose();
this.pubsub.unsubscribe(subId);
});
return Promise.all([transport.getCredentials(), this.getCachedNonExecutableSchema()]).then(([creds, schema]) => {
transport.processDirectives({ schema, creds });
return {
schema,
};
});
}
}
exports.default = GrpcHandler;