UNPKG

@actonate/mirkwood

Version:

GraphQL based Rapid Server-side Development framework

179 lines (129 loc) 6.68 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /** * Initialize all Components */ // GraphStack libraries var _express = require('express'); var _express2 = _interopRequireDefault(_express); var _bodyParser = require('body-parser'); var _bodyParser2 = _interopRequireDefault(_bodyParser); var _expressSession = require('express-session'); var _expressSession2 = _interopRequireDefault(_expressSession); var _cors = require('cors'); var _cors2 = _interopRequireDefault(_cors); var _apolloServerExpress = require('apollo-server-express'); var _apolloEngine = require('apollo-engine'); var _apolloErrors = require('apollo-errors'); var _gql = require('./gql'); var _gql2 = _interopRequireDefault(_gql); var _schemagen = require('./schemagen'); var _schemagen2 = _interopRequireDefault(_schemagen); var _sessionStore = require('./sessionStore'); var _sessionStore2 = _interopRequireDefault(_sessionStore); var _authenticator = require('./authenticator'); var _authenticator2 = _interopRequireDefault(_authenticator); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var NODE_ENV = process.env.NODE_ENV; var GraphQLServer = function () { function GraphQLServer(_ref) { var config = _ref.config, models = _ref.models, opts = _ref.opts; _classCallCheck(this, GraphQLServer); this.models = models; this.opts = opts; this.config = config; this.opts.cors = _extends({ origin: '*', credentials: true }, this.opts.cors); this.schemaGenerator = new _schemagen2.default({ config: config }); _authenticator2.default.init({ config: config.auth }); } _createClass(GraphQLServer, [{ key: '_generateSchema', value: function _generateSchema() { return this.schemaGenerator.generate(this.models); } }, { key: '_getSchemaContext', value: function _getSchemaContext() { return this.schemaGenerator.getSchemaContext(); } }, { key: 'start', value: function start(app, middlewares) { var _this = this; var APOLLO_ENGINE_API_KEY = process.env.APOLLO_ENGINE_API_KEY; var QUERY_CACHE_MAX_AGE = process.env.QUERY_CACHE_MAX_AGE; var _generateSchema2 = this._generateSchema(), schema = _generateSchema2.schema, internalSchema = _generateSchema2.internalSchema; var is_app_init = app ? true : false; // Initialize express app app = app || (0, _express2.default)(); app.use(_bodyParser2.default.urlencoded({ extended: false })); app.use(_bodyParser2.default.json()); app.use((0, _expressSession2.default)(new _sessionStore2.default({ config: this.config.session }))); app.use((0, _cors2.default)(this.opts.cors)); if (middlewares && middlewares.length) { for (var idx = 0; idx < middlewares.length; idx++) { var eachMiddleware = middlewares[idx]; app.use(eachMiddleware); } } app.post('/graphql', (0, _apolloServerExpress.graphqlExpress)(function (req, res) { var schemaContext = _this._getSchemaContext(); var gql = new _gql2.default(_extends({ schema: internalSchema, req: req }, schemaContext)); if (NODE_ENV === 'production') { return { schema: schema, formatError: _apolloErrors.formatError, // error formatting via apollo-errors tracing: true, cacheControl: { defaultMaxAge: Number.isInteger(QUERY_CACHE_MAX_AGE) ? parseInt(QUERY_CACHE_MAX_AGE) : 1800 }, context: _extends({ req: req, gql: gql }, schemaContext) }; } else { return { schema: internalSchema, formatError: _apolloErrors.formatError, // error formatting via apollo-errors tracing: true, cacheControl: { defaultMaxAge: Number.isInteger(QUERY_CACHE_MAX_AGE) ? parseInt(QUERY_CACHE_MAX_AGE) : 1800 }, context: _extends({ req: req, gql: gql }, schemaContext) }; } })); if (process.env.NODE_ENV !== 'production') { app.get('/graphiql', (0, _apolloServerExpress.graphiqlExpress)({ endpointURL: '/graphql' })); } // Initialize engine with API key. Alternatively, if (APOLLO_ENGINE_API_KEY) { var engine = new _apolloEngine.ApolloEngine({ apiKey: APOLLO_ENGINE_API_KEY }); console.log('Mirkwood server (with Engine) started on', this.opts.port); engine.listen({ port: this.opts.port, expressApp: app }); return; } console.log('Mirkwood server started on', this.opts.port); app.listen(this.opts.port); } }]); return GraphQLServer; }(); exports.default = GraphQLServer;