UNPKG

9.07 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 if (!context.completed) {
65 context.end(error);
66 }
67 return error;
68}
69exports.formatError = formatError;
70function formatResponse(context, response, end = true) {
71 if (typeof response.errors !== 'undefined') {
72 response.errors = response.errors.map(error => formatError(context, error));
73 }
74 context.event.emit('data', response);
75 if (end === true) {
76 context.end();
77 }
78 return response;
79}
80exports.formatResponse = formatResponse;
81function executeWithContext(schema, document, rootValue = {}, context, variableValues, operationName, fieldResolver) {
82 return __awaiter(this, void 0, void 0, function* () {
83 const params = {
84 document,
85 context,
86 variableValues,
87 operationName,
88 };
89 let result;
90 try {
91 result = yield graphql_1.execute(context.schema, document, rootValue, context, variableValues, operationName, fieldResolver);
92 result = formatResponse(context, result);
93 }
94 catch (error) {
95 throw formatError(context, error);
96 }
97 return result;
98 });
99}
100exports.executeWithContext = executeWithContext;
101function execute(document, params, variableValues, operationName) {
102 return __awaiter(this, void 0, void 0, function* () {
103 let result;
104 result = yield execute_1.default.filter(undefined, { document, params, variableValues, operationName });
105 if (typeof result === 'undefined') {
106 let operationType;
107 if (document.definitions[0].kind === 'OperationDefinition') {
108 operationType = document.definitions[0].operation;
109 }
110 const context = yield context_1.createContext(params, operationName, operationType);
111 result = yield executeWithContext(getSchema_1.getSchema(), document, {}, context, variableValues, operationName);
112 }
113 return result;
114 });
115}
116exports.execute = execute;
117function subscribeWithContext(schema, document, rootValue = {}, context, variableValues, operationName) {
118 return __awaiter(this, void 0, void 0, function* () {
119 let result;
120 result = yield graphql_1.subscribe(context.schema, document, rootValue, context, variableValues, operationName);
121 if (iterall_1.isAsyncIterable(result)) {
122 const iterable = result;
123 try {
124 result = (function () {
125 return __asyncGenerator(this, arguments, function* () {
126 try {
127 try {
128 for (var iterable_1 = __asyncValues(iterable), iterable_1_1; iterable_1_1 = yield __await(iterable_1.next()), !iterable_1_1.done;) {
129 const data = yield __await(iterable_1_1.value);
130 yield formatResponse(context, data, false);
131 }
132 }
133 catch (e_1_1) { e_1 = { error: e_1_1 }; }
134 finally {
135 try {
136 if (iterable_1_1 && !iterable_1_1.done && (_a = iterable_1.return)) yield __await(_a.call(iterable_1));
137 }
138 finally { if (e_1) throw e_1.error; }
139 }
140 if (!context.completed) {
141 context.end();
142 }
143 }
144 catch (error) {
145 throw formatError(context, error);
146 }
147 var e_1, _a;
148 });
149 })();
150 }
151 catch (error) {
152 throw formatError(context, error);
153 }
154 }
155 else {
156 try {
157 result = formatResponse(context, (yield result));
158 }
159 catch (error) {
160 throw formatError(context, error);
161 }
162 }
163 return result;
164 });
165}
166exports.subscribeWithContext = subscribeWithContext;
167function subscribe(document, params, variableValues, operationName) {
168 return __awaiter(this, void 0, void 0, function* () {
169 let result;
170 result = yield subscribe_1.default.filter(undefined, { document, params, variableValues, operationName });
171 if (typeof result === 'undefined') {
172 let operationType;
173 if (document.definitions[0].kind === 'OperationDefinition') {
174 operationType = document.definitions[0].operation;
175 }
176 const context = yield context_1.createContext(params, operationName, operationType);
177 result = yield subscribeWithContext(getSchema_1.getSchema(), document, {}, context, variableValues, operationName);
178 }
179 return result;
180 });
181}
182exports.subscribe = subscribe;
183function bootstrap() {
184 return __awaiter(this, void 0, void 0, function* () {
185 require('./loadConfig');
186 if (process.env.CORTEXQL_COMMAND === 'start'
187 || process.env.CORTEXQL_COMMAND === 'develop') {
188 require('./server');
189 }
190 if (config_1.default.disableSubscription) {
191 hooks.bootstrap.disable('subscription');
192 }
193 if (!hooks.bootstrap.has('server')) {
194 console.log('Server is disabled you should only use request or subscribe methods internaly');
195 }
196 yield getSchema_1.getSchema();
197 yield hooks.bootstrap.do({});
198 yield hooks.init.do({});
199 });
200}
201exports.bootstrap = bootstrap;
202//# sourceMappingURL=index.js.map
\No newline at end of file