UNPKG

@girin/framework

Version:

Core modules for Girin: GraphQL server framework

90 lines 4.17 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const environment_1 = require("@girin/environment"); const typelink_1 = require("@girin/typelink"); const http = __importStar(require("http")); const apollo_server_express_1 = require("apollo-server-express"); const graphql_1 = require("graphql"); const SchemaBuilder_1 = require("./SchemaBuilder"); const global_1 = require("../global"); class HttpServer extends environment_1.Module { constructor(configs) { super(); this.configs = configs; } get label() { return 'HttpServer'; } onInit() { const schemaModule = SchemaBuilder_1.SchemaBuilder.object(); typelink_1.getGlobalMetadataStorage().registerMetadata([ new typelink_1.GraphQLTypeIndex({ typeInstance: apollo_server_express_1.GraphQLUpload, definitionClass: null }), ]); const CacheControlEnum = new graphql_1.GraphQLEnumType({ name: 'CacheControlScope', values: { PUBLIC: {}, PRIVATE: {} }, }); schemaModule.schemaOptions.types.push(CacheControlEnum); schemaModule.schemaOptions.directives.push(new graphql_1.GraphQLDirective({ name: 'cacheControl', locations: ['FIELD_DEFINITION', 'OBJECT', 'INTERFACE'], args: { maxAge: { type: graphql_1.GraphQLInt }, scope: { type: CacheControlEnum }, } })); } onBootstrap() { return __awaiter(this, void 0, void 0, function* () { yield SchemaBuilder_1.SchemaBuilder.bootstrap(); const { configs } = this; const { schema } = SchemaBuilder_1.SchemaBuilder.object(); const apolloServerConfig = this.configs.apollo || {}; // build context function from global contextMap const context = (ctx) => __awaiter(this, void 0, void 0, function* () { const results = Object.assign({}, ctx); for (let [fieldName, ctxFn] of global_1.contextMap.entries()) { results[fieldName] = yield ctxFn(ctx); } return results; }); const apolloServer = new apollo_server_express_1.ApolloServer(Object.assign({}, apolloServerConfig, { schema, context })); apolloServer.applyMiddleware({ app: global_1.app, path: '/graphql', bodyParserConfig: { limit: '50mb' }, cors: typeof configs.cors !== 'undefined' ? configs.cors : { origin: '*' }, }); this.httpServer = http.createServer(global_1.app); if (apolloServerConfig.subscriptions) { apolloServer.installSubscriptionHandlers(this.httpServer); } return new Promise((resolve, reject) => { this.httpServer.once('listening', () => resolve()); this.httpServer.on('error', err => reject(err)); this.httpServer.listen(configs.listen); }); }); } onDestroy() { return __awaiter(this, void 0, void 0, function* () { yield this.httpServer.close(); }); } } exports.HttpServer = HttpServer; //# sourceMappingURL=HttpServer.js.map