UNPKG

@cortexql/core

Version:

A RESTful API framework for your apps based on GraphQL type system.

202 lines 9.1 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 __asyncValues = (this && this.__asyncValues) || function (o) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var m = o[Symbol.asyncIterator]; return m ? m.call(o) : typeof __values === "function" ? __values(o) : o[Symbol.iterator](); }; var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); } var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var g = generator.apply(thisArg, _arguments || []), i, q = []; return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i; function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; } function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } } function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); } function fulfill(value) { resume("next", value); } function reject(value) { resume("throw", value); } function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); } }; function __export(m) { for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; } Object.defineProperty(exports, "__esModule", { value: true }); const hooks = require("./hooks"); exports.hooks = hooks; const getSchema_1 = require("./api/getSchema"); const config_1 = require("./config"); const graphql_1 = require("graphql"); const execute_1 = require("./hooks/execute"); const subscribe_1 = require("./hooks/subscribe"); const context_1 = require("./api/context"); const iterall_1 = require("iterall"); __export(require("./api/utils")); var graphql_tag_1 = require("graphql-tag"); exports.gql = graphql_tag_1.default; var graphql_2 = require("graphql"); exports.GraphQLError = graphql_2.GraphQLError; __export(require("./api/context")); function getOriginalError(error) { if (!(error instanceof graphql_1.GraphQLError)) { return error; } if (error.originalError instanceof graphql_1.GraphQLError) { return getOriginalError(error.originalError); } return error.originalError; } function formatError(context, error) { const originalError = getOriginalError(error); if (typeof originalError !== 'undefined') { console.log(originalError); console.log(originalError.stack !== undefined ? originalError.stack : originalError.message); return new graphql_1.GraphQLError('An internal server error ocurred'); } if (!context.completed) { context.end(error); } return error; } exports.formatError = formatError; function formatResponse(context, response, end = true) { if (typeof response.errors !== 'undefined') { response.errors = response.errors.map(error => formatError(context, error)); } context.event.emit('data', response); if (end === true && !context.completed) { context.end(); } return response; } exports.formatResponse = formatResponse; function executeWithContext(schema, document, rootValue = {}, context, variableValues, operationName, fieldResolver) { return __awaiter(this, void 0, void 0, function* () { const params = { document, context, variableValues, operationName, }; let result; try { result = yield graphql_1.execute(context.schema, document, rootValue, context, variableValues, operationName, fieldResolver); result = formatResponse(context, result); } catch (error) { throw formatError(context, error); } return result; }); } exports.executeWithContext = executeWithContext; function execute(document, params, variableValues, operationName) { return __awaiter(this, void 0, void 0, function* () { let result; result = yield execute_1.default.filter(undefined, { document, params, variableValues, operationName }); if (typeof result === 'undefined') { let operationType; if (document.definitions[0].kind === 'OperationDefinition') { operationType = document.definitions[0].operation; } const context = yield context_1.createContext(params, operationName, operationType); result = yield executeWithContext(getSchema_1.getSchema(), document, {}, context, variableValues, operationName); } return result; }); } exports.execute = execute; function subscribeWithContext(schema, document, rootValue = {}, context, variableValues, operationName) { return __awaiter(this, void 0, void 0, function* () { let result; result = yield graphql_1.subscribe(context.schema, document, rootValue, context, variableValues, operationName); if (iterall_1.isAsyncIterable(result)) { const iterable = result; try { result = (function () { return __asyncGenerator(this, arguments, function* () { try { try { for (var iterable_1 = __asyncValues(iterable), iterable_1_1; iterable_1_1 = yield __await(iterable_1.next()), !iterable_1_1.done;) { const data = yield __await(iterable_1_1.value); yield formatResponse(context, data, false); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (iterable_1_1 && !iterable_1_1.done && (_a = iterable_1.return)) yield __await(_a.call(iterable_1)); } finally { if (e_1) throw e_1.error; } } if (!context.completed) { context.end(); } } catch (error) { throw formatError(context, error); } var e_1, _a; }); })(); } catch (error) { throw formatError(context, error); } } else { try { result = formatResponse(context, (yield result)); } catch (error) { throw formatError(context, error); } } return result; }); } exports.subscribeWithContext = subscribeWithContext; function subscribe(document, params, variableValues, operationName) { return __awaiter(this, void 0, void 0, function* () { let result; result = yield subscribe_1.default.filter(undefined, { document, params, variableValues, operationName }); if (typeof result === 'undefined') { let operationType; if (document.definitions[0].kind === 'OperationDefinition') { operationType = document.definitions[0].operation; } const context = yield context_1.createContext(params, operationName, operationType); result = yield subscribeWithContext(getSchema_1.getSchema(), document, {}, context, variableValues, operationName); } return result; }); } exports.subscribe = subscribe; function bootstrap() { return __awaiter(this, void 0, void 0, function* () { require('./loadConfig'); if (process.env.CORTEXQL_COMMAND === 'start' || process.env.CORTEXQL_COMMAND === 'develop') { require('./server'); } if (config_1.default.disableSubscription) { hooks.bootstrap.disable('subscription'); } if (!hooks.bootstrap.has('server')) { console.log('Server is disabled you should only use request or subscribe methods internaly'); } yield getSchema_1.getSchema(); yield hooks.bootstrap.do({}); yield hooks.init.do({}); }); } exports.bootstrap = bootstrap; //# sourceMappingURL=index.js.map