UNPKG

bit-bin

Version:

<a href="https://opensource.org/licenses/Apache-2.0"><img alt="apache" src="https://img.shields.io/badge/License-Apache%202.0-blue.svg"></a> <a href="https://github.com/teambit/bit/blob/master/CONTRIBUTING.md"><img alt="prs" src="https://img.shields.io/b

141 lines (105 loc) 2.71 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.GraphQLExtension = void 0; function _bluebird() { const data = require("bluebird"); _bluebird = function () { return data; }; return data; } function _defineProperty2() { const data = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); _defineProperty2 = function () { return data; }; return data; } function _express() { const data = _interopRequireDefault(require("express")); _express = function () { return data; }; return data; } function _cors() { const data = _interopRequireDefault(require("cors")); _cors = function () { return data; }; return data; } function _expressGraphql() { const data = _interopRequireDefault(require("express-graphql")); _expressGraphql = function () { return data; }; return data; } function _harmony() { const data = require("@teambit/harmony"); _harmony = function () { return data; }; return data; } function _core() { const data = require("@graphql-modules/core"); _core = function () { return data; }; return data; } class GraphQLExtension { constructor(config, moduleSlot) { this.config = config; this.moduleSlot = moduleSlot; } createRootModule(modules) { return new (_core().GraphQLModule)({ imports: modules }); } /** * start a graphql server. */ listen(port) { var _this = this; return (0, _bluebird().coroutine)(function* () { const modules = _this.moduleSlot.values(); const schema = _this.createRootModule(modules); const app = (0, _express().default)(); app.use((0, _cors().default)()); app.use('/graphql', (0, _expressGraphql().default)({ schema: schema.schema, graphiql: true })); app.listen(port || _this.config.port); })(); } getSchema() { const modules = this.moduleSlot.values(); return this.createRootModule(modules); } /** * register a new graphql module. */ register(module) { this.moduleSlot.register(module); return this; } static provider(deps, config, [moduleSlot]) { return (0, _bluebird().coroutine)(function* () { return new GraphQLExtension(config, moduleSlot); })(); } } exports.GraphQLExtension = GraphQLExtension; (0, _defineProperty2().default)(GraphQLExtension, "dependencies", []); (0, _defineProperty2().default)(GraphQLExtension, "slots", [_harmony().Slot.withType()]); (0, _defineProperty2().default)(GraphQLExtension, "defaultConfig", { port: 4000 });