UNPKG

9.15 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 return new (P || (P = Promise))(function (resolve, reject) {
4 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7 step((generator = generator.apply(thisArg, _arguments || [])).next());
8 });
9};
10var __asyncValues = (this && this.__asyncValues) || function (o) {
11 if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
12 var m = o[Symbol.asyncIterator];
13 return m ? m.call(o) : typeof __values === "function" ? __values(o) : o[Symbol.iterator]();
14};
15var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
16var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
17 if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
18 var g = generator.apply(thisArg, _arguments || []), i, q = [];
19 return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
20 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); }); }; }
21 function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
22 function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
23 function fulfill(value) { resume("next", value); }
24 function reject(value) { resume("throw", value); }
25 function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
26};
27function __export(m) {
28 for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
29}
30Object.defineProperty(exports, "__esModule", { value: true });
31const hooks = require("./hooks");
32exports.hooks = hooks;
33const getSchema_1 = require("./api/getSchema");
34const config_1 = require("./config");
35const graphql_1 = require("graphql");
36const execute_1 = require("./hooks/execute");
37const subscribe_1 = require("./hooks/subscribe");
38const context_1 = require("./api/context");
39const iterall_1 = require("iterall");
40__export(require("./api/utils"));
41var graphql_tag_1 = require("graphql-tag");
42exports.gql = graphql_tag_1.default;
43var graphql_2 = require("graphql");
44exports.GraphQLError = graphql_2.GraphQLError;
45__export(require("./api/context"));
46function getOriginalError(error) {
47 if (!(error instanceof graphql_1.GraphQLError)) {
48 return error;
49 }
50 if (error.originalError instanceof graphql_1.GraphQLError) {
51 return getOriginalError(error.originalError);
52 }
53 return error.originalError;
54}
55function formatError(context, error) {
56 const originalError = getOriginalError(error);
57 if (typeof originalError !== 'undefined') {
58 console.log(originalError);
59 console.log(originalError.stack !== undefined
60 ? originalError.stack
61 : originalError.message);
62 return new graphql_1.GraphQLError('An internal server error ocurred');
63 }
64 try {
65 context.end(error);
66 }
67 catch (eventError) {
68 // TODO: Check why it's throwing error when emitting
69 }
70 return error;
71}
72exports.formatError = formatError;
73function formatResponse(context, response, end = true) {
74 if (typeof response.errors !== 'undefined') {
75 response.errors = response.errors.map(error => formatError(context, error));
76 }
77 context.event.emit('data', response);
78 if (end === true) {
79 context.end();
80 }
81 return response;
82}
83exports.formatResponse = formatResponse;
84function executeWithContext(schema, document, rootValue = {}, context, variableValues, operationName, fieldResolver) {
85 return __awaiter(this, void 0, void 0, function* () {
86 const params = {
87 document,
88 context,
89 variableValues,
90 operationName,
91 };
92 let result;
93 try {
94 result = yield graphql_1.execute(context.schema, document, rootValue, context, variableValues, operationName, fieldResolver);
95 result = formatResponse(context, result);
96 }
97 catch (error) {
98 throw formatError(context, error);
99 }
100 return result;
101 });
102}
103exports.executeWithContext = executeWithContext;
104function execute(document, params, variableValues, operationName) {
105 return __awaiter(this, void 0, void 0, function* () {
106 let result;
107 result = yield execute_1.default.filter(undefined, { document, params, variableValues, operationName });
108 if (typeof result === 'undefined') {
109 let operationType;
110 if (document.definitions[0].kind === 'OperationDefinition') {
111 operationType = document.definitions[0].operation;
112 }
113 const context = yield context_1.createContext(params, operationName, operationType);
114 result = yield executeWithContext(getSchema_1.getSchema(), document, {}, context, variableValues, operationName);
115 }
116 return result;
117 });
118}
119exports.execute = execute;
120function subscribeWithContext(schema, document, rootValue = {}, context, variableValues, operationName) {
121 return __awaiter(this, void 0, void 0, function* () {
122 let result;
123 result = yield graphql_1.subscribe(context.schema, document, rootValue, context, variableValues, operationName);
124 if (iterall_1.isAsyncIterable(result)) {
125 const iterable = result;
126 try {
127 result = (function () {
128 return __asyncGenerator(this, arguments, function* () {
129 try {
130 try {
131 for (var iterable_1 = __asyncValues(iterable), iterable_1_1; iterable_1_1 = yield __await(iterable_1.next()), !iterable_1_1.done;) {
132 const data = yield __await(iterable_1_1.value);
133 yield formatResponse(context, data, false);
134 }
135 }
136 catch (e_1_1) { e_1 = { error: e_1_1 }; }
137 finally {
138 try {
139 if (iterable_1_1 && !iterable_1_1.done && (_a = iterable_1.return)) yield __await(_a.call(iterable_1));
140 }
141 finally { if (e_1) throw e_1.error; }
142 }
143 if (!context.completed) {
144 context.end();
145 }
146 }
147 catch (error) {
148 throw formatError(context, error);
149 }
150 var e_1, _a;
151 });
152 })();
153 }
154 catch (error) {
155 throw formatError(context, error);
156 }
157 }
158 else {
159 try {
160 result = formatResponse(context, (yield result));
161 }
162 catch (error) {
163 throw formatError(context, error);
164 }
165 }
166 return result;
167 });
168}
169exports.subscribeWithContext = subscribeWithContext;
170function subscribe(document, params, variableValues, operationName) {
171 return __awaiter(this, void 0, void 0, function* () {
172 let result;
173 result = yield subscribe_1.default.filter(undefined, { document, params, variableValues, operationName });
174 if (typeof result === 'undefined') {
175 let operationType;
176 if (document.definitions[0].kind === 'OperationDefinition') {
177 operationType = document.definitions[0].operation;
178 }
179 const context = yield context_1.createContext(params, operationName, operationType);
180 result = yield subscribeWithContext(getSchema_1.getSchema(), document, {}, context, variableValues, operationName);
181 }
182 return result;
183 });
184}
185exports.subscribe = subscribe;
186function bootstrap() {
187 return __awaiter(this, void 0, void 0, function* () {
188 require('./loadConfig');
189 if (process.env.CORTEXQL_COMMAND === 'start'
190 || process.env.CORTEXQL_COMMAND === 'develop') {
191 require('./server');
192 }
193 if (config_1.default.disableSubscription) {
194 hooks.bootstrap.disable('subscription');
195 }
196 if (!hooks.bootstrap.has('server')) {
197 console.log('Server is disabled you should only use request or subscribe methods internaly');
198 }
199 yield getSchema_1.getSchema();
200 yield hooks.bootstrap.do({});
201 yield hooks.init.do({});
202 });
203}
204exports.bootstrap = bootstrap;
205//# sourceMappingURL=index.js.map
\No newline at end of file