1 | "use strict";
|
2 | var __extends = (this && this.__extends) || (function () {
|
3 | var extendStatics = function (d, b) {
|
4 | extendStatics = Object.setPrototypeOf ||
|
5 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
6 | function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
7 | return extendStatics(d, b);
|
8 | };
|
9 | return function (d, b) {
|
10 | extendStatics(d, b);
|
11 | function __() { this.constructor = d; }
|
12 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
13 | };
|
14 | })();
|
15 | var __assign = (this && this.__assign) || function () {
|
16 | __assign = Object.assign || function(t) {
|
17 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
18 | s = arguments[i];
|
19 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
20 | t[p] = s[p];
|
21 | }
|
22 | return t;
|
23 | };
|
24 | return __assign.apply(this, arguments);
|
25 | };
|
26 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
27 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
28 | return new (P || (P = Promise))(function (resolve, reject) {
|
29 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
30 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
31 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
32 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
33 | });
|
34 | };
|
35 | var __generator = (this && this.__generator) || function (thisArg, body) {
|
36 | var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
37 | return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
38 | function verb(n) { return function (v) { return step([n, v]); }; }
|
39 | function step(op) {
|
40 | if (f) throw new TypeError("Generator is already executing.");
|
41 | while (_) try {
|
42 | if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
43 | if (y = 0, t) op = [op[0] & 2, t.value];
|
44 | switch (op[0]) {
|
45 | case 0: case 1: t = op; break;
|
46 | case 4: _.label++; return { value: op[1], done: false };
|
47 | case 5: _.label++; y = op[1]; op = [0]; continue;
|
48 | case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
49 | default:
|
50 | if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
51 | if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
52 | if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
53 | if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
54 | if (t[2]) _.ops.pop();
|
55 | _.trys.pop(); continue;
|
56 | }
|
57 | op = body.call(thisArg, _);
|
58 | } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
59 | if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
60 | }
|
61 | };
|
62 | var __rest = (this && this.__rest) || function (s, e) {
|
63 | var t = {};
|
64 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
65 | t[p] = s[p];
|
66 | if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
67 | for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
68 | if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
69 | t[p[i]] = s[p[i]];
|
70 | }
|
71 | return t;
|
72 | };
|
73 | var __asyncValues = (this && this.__asyncValues) || function (o) {
|
74 | if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
75 | var m = o[Symbol.asyncIterator], i;
|
76 | return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
77 | function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
78 | function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
79 | };
|
80 | var __read = (this && this.__read) || function (o, n) {
|
81 | var m = typeof Symbol === "function" && o[Symbol.iterator];
|
82 | if (!m) return o;
|
83 | var i = m.call(o), r, ar = [], e;
|
84 | try {
|
85 | while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
86 | }
|
87 | catch (error) { e = { error: error }; }
|
88 | finally {
|
89 | try {
|
90 | if (r && !r.done && (m = i["return"])) m.call(i);
|
91 | }
|
92 | finally { if (e) throw e.error; }
|
93 | }
|
94 | return ar;
|
95 | };
|
96 | var __spread = (this && this.__spread) || function () {
|
97 | for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
|
98 | return ar;
|
99 | };
|
100 | var __values = (this && this.__values) || function(o) {
|
101 | var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
102 | if (m) return m.call(o);
|
103 | if (o && typeof o.length === "number") return {
|
104 | next: function () {
|
105 | if (o && i >= o.length) o = void 0;
|
106 | return { value: o && o[i++], done: !o };
|
107 | }
|
108 | };
|
109 | throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
110 | };
|
111 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
112 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
113 | };
|
114 | Object.defineProperty(exports, "__esModule", { value: true });
|
115 | exports.GraphiQL = void 0;
|
116 | var react_1 = __importDefault(require("react"));
|
117 | var graphql_1 = require("graphql");
|
118 | var copy_to_clipboard_1 = __importDefault(require("copy-to-clipboard"));
|
119 | var graphql_language_service_utils_1 = require("graphql-language-service-utils");
|
120 | var ExecuteButton_1 = require("./ExecuteButton");
|
121 | var ImagePreview_1 = require("./ImagePreview");
|
122 | var ToolbarButton_1 = require("./ToolbarButton");
|
123 | var ToolbarGroup_1 = require("./ToolbarGroup");
|
124 | var ToolbarMenu_1 = require("./ToolbarMenu");
|
125 | var QueryEditor_1 = require("./QueryEditor");
|
126 | var VariableEditor_1 = require("./VariableEditor");
|
127 | var HeaderEditor_1 = require("./HeaderEditor");
|
128 | var ResultViewer_1 = require("./ResultViewer");
|
129 | var DocExplorer_1 = require("./DocExplorer");
|
130 | var QueryHistory_1 = require("./QueryHistory");
|
131 | var CodeMirrorSizer_1 = __importDefault(require("../utility/CodeMirrorSizer"));
|
132 | var StorageAPI_1 = __importDefault(require("../utility/StorageAPI"));
|
133 | var getQueryFacts_1 = __importDefault(require("../utility/getQueryFacts"));
|
134 | var getSelectedOperationName_1 = __importDefault(require("../utility/getSelectedOperationName"));
|
135 | var debounce_1 = __importDefault(require("../utility/debounce"));
|
136 | var find_1 = __importDefault(require("../utility/find"));
|
137 | var fillLeafs_1 = require("../utility/fillLeafs");
|
138 | var elementPosition_1 = require("../utility/elementPosition");
|
139 | var mergeAst_1 = __importDefault(require("../utility/mergeAst"));
|
140 | var introspectionQueries_1 = require("../utility/introspectionQueries");
|
141 | var merge_1 = require("dset/merge");
|
142 | var DEFAULT_DOC_EXPLORER_WIDTH = 350;
|
143 | var majorVersion = parseInt(react_1.default.version.slice(0, 2), 10);
|
144 | if (majorVersion < 16) {
|
145 | throw Error([
|
146 | 'GraphiQL 0.18.0 and after is not compatible with React 15 or below.',
|
147 | 'If you are using a CDN source (jsdelivr, unpkg, etc), follow this example:',
|
148 | 'https://github.com/graphql/graphiql/blob/master/examples/graphiql-cdn/index.html#L49',
|
149 | ].join('\n'));
|
150 | }
|
151 | var GraphiQL = (function (_super) {
|
152 | __extends(GraphiQL, _super);
|
153 | function GraphiQL(props) {
|
154 | var _a, _b;
|
155 | var _this = _super.call(this, props) || this;
|
156 | _this._editorQueryID = 0;
|
157 | _this.safeSetState = function (nextState, callback) {
|
158 | _this.componentIsMounted && _this.setState(nextState, callback);
|
159 | };
|
160 | _this.handleClickReference = function (reference) {
|
161 | _this.setState({ docExplorerOpen: true }, function () {
|
162 | if (_this.docExplorerComponent) {
|
163 | _this.docExplorerComponent.showDocForReference(reference);
|
164 | }
|
165 | });
|
166 | _this._storage.set('docExplorerOpen', JSON.stringify(_this.state.docExplorerOpen));
|
167 | };
|
168 | _this.handleRunQuery = function (selectedOperationName) { return __awaiter(_this, void 0, void 0, function () {
|
169 | var queryID, editedQuery, variables, headers, shouldPersistHeaders, operationName, fullResponse_1, subscription, error_1;
|
170 | var _this = this;
|
171 | return __generator(this, function (_a) {
|
172 | switch (_a.label) {
|
173 | case 0:
|
174 | this._editorQueryID++;
|
175 | queryID = this._editorQueryID;
|
176 | editedQuery = this.autoCompleteLeafs() || this.state.query;
|
177 | variables = this.state.variables;
|
178 | headers = this.state.headers;
|
179 | shouldPersistHeaders = this.state.shouldPersistHeaders;
|
180 | operationName = this.state.operationName;
|
181 | if (selectedOperationName && selectedOperationName !== operationName) {
|
182 | operationName = selectedOperationName;
|
183 | this.handleEditOperationName(operationName);
|
184 | }
|
185 | _a.label = 1;
|
186 | case 1:
|
187 | _a.trys.push([1, 3, , 4]);
|
188 | this.setState({
|
189 | isWaitingForResponse: true,
|
190 | response: undefined,
|
191 | operationName: operationName,
|
192 | });
|
193 | this._storage.set('operationName', operationName);
|
194 | if (this._queryHistory) {
|
195 | this._queryHistory.updateHistory(editedQuery, variables, headers, operationName);
|
196 | }
|
197 | fullResponse_1 = { data: {} };
|
198 | return [4, this._fetchQuery(editedQuery, variables, headers, operationName, shouldPersistHeaders, function (result) {
|
199 | var e_1, _a;
|
200 | if (queryID === _this._editorQueryID) {
|
201 | var maybeMultipart = Array.isArray(result) ? result : false;
|
202 | if (!maybeMultipart &&
|
203 | typeof result !== 'string' &&
|
204 | result !== null &&
|
205 | 'hasNext' in result) {
|
206 | maybeMultipart = [result];
|
207 | }
|
208 | if (maybeMultipart) {
|
209 | var payload = { data: fullResponse_1.data };
|
210 | var maybeErrors = __spread(((fullResponse_1 === null || fullResponse_1 === void 0 ? void 0 : fullResponse_1.errors) || []), maybeMultipart
|
211 | .map(function (i) { return i.errors; })
|
212 | .flat()
|
213 | .filter(Boolean));
|
214 | if (maybeErrors.length) {
|
215 | payload.errors = maybeErrors;
|
216 | }
|
217 | try {
|
218 | for (var maybeMultipart_1 = __values(maybeMultipart), maybeMultipart_1_1 = maybeMultipart_1.next(); !maybeMultipart_1_1.done; maybeMultipart_1_1 = maybeMultipart_1.next()) {
|
219 | var part = maybeMultipart_1_1.value;
|
220 | var path = part.path, data = part.data, _errors = part.errors, rest = __rest(part, ["path", "data", "errors"]);
|
221 | if (path) {
|
222 | if (!data) {
|
223 | throw new Error("Expected part to contain a data property, but got " + part);
|
224 | }
|
225 | merge_1.dset(payload.data, path, data);
|
226 | }
|
227 | else if (data) {
|
228 | payload.data = part.data;
|
229 | }
|
230 | fullResponse_1 = __assign(__assign({}, payload), rest);
|
231 | }
|
232 | }
|
233 | catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
234 | finally {
|
235 | try {
|
236 | if (maybeMultipart_1_1 && !maybeMultipart_1_1.done && (_a = maybeMultipart_1.return)) _a.call(maybeMultipart_1);
|
237 | }
|
238 | finally { if (e_1) throw e_1.error; }
|
239 | }
|
240 | _this.setState({
|
241 | isWaitingForResponse: false,
|
242 | response: GraphiQL.formatResult(fullResponse_1),
|
243 | });
|
244 | }
|
245 | else {
|
246 | _this.setState({
|
247 | isWaitingForResponse: false,
|
248 | response: GraphiQL.formatResult(result),
|
249 | });
|
250 | }
|
251 | }
|
252 | })];
|
253 | case 2:
|
254 | subscription = _a.sent();
|
255 | this.setState({ subscription: subscription });
|
256 | return [3, 4];
|
257 | case 3:
|
258 | error_1 = _a.sent();
|
259 | this.setState({
|
260 | isWaitingForResponse: false,
|
261 | response: error_1.message,
|
262 | });
|
263 | return [3, 4];
|
264 | case 4: return [2];
|
265 | }
|
266 | });
|
267 | }); };
|
268 | _this.handleStopQuery = function () {
|
269 | var subscription = _this.state.subscription;
|
270 | _this.setState({
|
271 | isWaitingForResponse: false,
|
272 | subscription: null,
|
273 | });
|
274 | if (subscription) {
|
275 | subscription.unsubscribe();
|
276 | }
|
277 | };
|
278 | _this.handlePrettifyQuery = function () {
|
279 | var _a, _b, _c;
|
280 | var editor = _this.getQueryEditor();
|
281 | var editorContent = (_a = editor === null || editor === void 0 ? void 0 : editor.getValue()) !== null && _a !== void 0 ? _a : '';
|
282 | var prettifiedEditorContent = graphql_1.print(graphql_1.parse(editorContent, { experimentalFragmentVariables: true }));
|
283 | if (prettifiedEditorContent !== editorContent) {
|
284 | editor === null || editor === void 0 ? void 0 : editor.setValue(prettifiedEditorContent);
|
285 | }
|
286 | var variableEditor = _this.getVariableEditor();
|
287 | var variableEditorContent = (_b = variableEditor === null || variableEditor === void 0 ? void 0 : variableEditor.getValue()) !== null && _b !== void 0 ? _b : '';
|
288 | try {
|
289 | var prettifiedVariableEditorContent = JSON.stringify(JSON.parse(variableEditorContent), null, 2);
|
290 | if (prettifiedVariableEditorContent !== variableEditorContent) {
|
291 | variableEditor === null || variableEditor === void 0 ? void 0 : variableEditor.setValue(prettifiedVariableEditorContent);
|
292 | }
|
293 | }
|
294 | catch (_d) {
|
295 | }
|
296 | var headerEditor = _this.getHeaderEditor();
|
297 | var headerEditorContent = (_c = headerEditor === null || headerEditor === void 0 ? void 0 : headerEditor.getValue()) !== null && _c !== void 0 ? _c : '';
|
298 | try {
|
299 | var prettifiedHeaderEditorContent = JSON.stringify(JSON.parse(headerEditorContent), null, 2);
|
300 | if (prettifiedHeaderEditorContent !== headerEditorContent) {
|
301 | headerEditor === null || headerEditor === void 0 ? void 0 : headerEditor.setValue(prettifiedHeaderEditorContent);
|
302 | }
|
303 | }
|
304 | catch (_e) {
|
305 | }
|
306 | };
|
307 | _this.handleMergeQuery = function () {
|
308 | var editor = _this.getQueryEditor();
|
309 | var query = editor.getValue();
|
310 | if (!query) {
|
311 | return;
|
312 | }
|
313 | var ast = _this.state.documentAST;
|
314 | editor.setValue(graphql_1.print(mergeAst_1.default(ast, _this.state.schema)));
|
315 | };
|
316 | _this.handleEditQuery = debounce_1.default(100, function (value) {
|
317 | var queryFacts = _this._updateQueryFacts(value, _this.state.operationName, _this.state.operations, _this.state.schema);
|
318 | _this.setState(__assign({ query: value }, queryFacts));
|
319 | _this._storage.set('query', value);
|
320 | if (_this.props.onEditQuery) {
|
321 | return _this.props.onEditQuery(value, queryFacts === null || queryFacts === void 0 ? void 0 : queryFacts.documentAST);
|
322 | }
|
323 | });
|
324 | _this.handleCopyQuery = function () {
|
325 | var editor = _this.getQueryEditor();
|
326 | var query = editor && editor.getValue();
|
327 | if (!query) {
|
328 | return;
|
329 | }
|
330 | copy_to_clipboard_1.default(query);
|
331 | if (_this.props.onCopyQuery) {
|
332 | return _this.props.onCopyQuery(query);
|
333 | }
|
334 | };
|
335 | _this._updateQueryFacts = function (query, operationName, prevOperations, schema) {
|
336 | var queryFacts = getQueryFacts_1.default(schema, query);
|
337 | if (queryFacts) {
|
338 | var updatedOperationName = getSelectedOperationName_1.default(prevOperations, operationName, queryFacts.operations);
|
339 | var onEditOperationName = _this.props.onEditOperationName;
|
340 | if (onEditOperationName &&
|
341 | updatedOperationName &&
|
342 | operationName !== updatedOperationName) {
|
343 | onEditOperationName(updatedOperationName);
|
344 | }
|
345 | return __assign({ operationName: updatedOperationName }, queryFacts);
|
346 | }
|
347 | };
|
348 | _this.handleEditVariables = function (value) {
|
349 | _this.setState({ variables: value });
|
350 | debounce_1.default(500, function () { return _this._storage.set('variables', value); })();
|
351 | if (_this.props.onEditVariables) {
|
352 | _this.props.onEditVariables(value);
|
353 | }
|
354 | };
|
355 | _this.handleEditHeaders = function (value) {
|
356 | _this.setState({ headers: value });
|
357 | _this.props.shouldPersistHeaders &&
|
358 | debounce_1.default(500, function () { return _this._storage.set('headers', value); })();
|
359 | if (_this.props.onEditHeaders) {
|
360 | _this.props.onEditHeaders(value);
|
361 | }
|
362 | };
|
363 | _this.handleEditOperationName = function (operationName) {
|
364 | var onEditOperationName = _this.props.onEditOperationName;
|
365 | if (onEditOperationName) {
|
366 | onEditOperationName(operationName);
|
367 | }
|
368 | };
|
369 | _this.handleHintInformationRender = function (elem) {
|
370 | elem.addEventListener('click', _this._onClickHintInformation);
|
371 | var onRemoveFn;
|
372 | elem.addEventListener('DOMNodeRemoved', (onRemoveFn = function () {
|
373 | elem.removeEventListener('DOMNodeRemoved', onRemoveFn);
|
374 | elem.removeEventListener('click', _this._onClickHintInformation);
|
375 | }));
|
376 | };
|
377 | _this.handleEditorRunQuery = function () {
|
378 | _this._runQueryAtCursor();
|
379 | };
|
380 | _this._onClickHintInformation = function (event) {
|
381 | if ((event === null || event === void 0 ? void 0 : event.currentTarget) &&
|
382 | 'className' in event.currentTarget &&
|
383 | event.currentTarget.className === 'typeName') {
|
384 | var typeName = event.currentTarget.innerHTML;
|
385 | var schema = _this.state.schema;
|
386 | if (schema) {
|
387 | var type_1 = schema.getType(typeName);
|
388 | if (type_1) {
|
389 | _this.setState({ docExplorerOpen: true }, function () {
|
390 | if (_this.docExplorerComponent) {
|
391 | _this.docExplorerComponent.showDoc(type_1);
|
392 | }
|
393 | });
|
394 | debounce_1.default(500, function () {
|
395 | return _this._storage.set('docExplorerOpen', JSON.stringify(_this.state.docExplorerOpen));
|
396 | })();
|
397 | }
|
398 | }
|
399 | }
|
400 | };
|
401 | _this.handleToggleDocs = function () {
|
402 | if (typeof _this.props.onToggleDocs === 'function') {
|
403 | _this.props.onToggleDocs(!_this.state.docExplorerOpen);
|
404 | }
|
405 | _this._storage.set('docExplorerOpen', JSON.stringify(!_this.state.docExplorerOpen));
|
406 | _this.setState({ docExplorerOpen: !_this.state.docExplorerOpen });
|
407 | };
|
408 | _this.handleToggleHistory = function () {
|
409 | if (typeof _this.props.onToggleHistory === 'function') {
|
410 | _this.props.onToggleHistory(!_this.state.historyPaneOpen);
|
411 | }
|
412 | _this._storage.set('historyPaneOpen', JSON.stringify(!_this.state.historyPaneOpen));
|
413 | _this.setState({ historyPaneOpen: !_this.state.historyPaneOpen });
|
414 | };
|
415 | _this.handleSelectHistoryQuery = function (query, variables, headers, operationName) {
|
416 | if (query) {
|
417 | _this.handleEditQuery(query);
|
418 | }
|
419 | if (variables) {
|
420 | _this.handleEditVariables(variables);
|
421 | }
|
422 | if (headers) {
|
423 | _this.handleEditHeaders(headers);
|
424 | }
|
425 | if (operationName) {
|
426 | _this.handleEditOperationName(operationName);
|
427 | }
|
428 | };
|
429 | _this.handleResizeStart = function (downEvent) {
|
430 | if (!_this._didClickDragBar(downEvent)) {
|
431 | return;
|
432 | }
|
433 | downEvent.preventDefault();
|
434 | var offset = downEvent.clientX - elementPosition_1.getLeft(downEvent.target);
|
435 | var onMouseMove = function (moveEvent) {
|
436 | if (moveEvent.buttons === 0) {
|
437 | return onMouseUp();
|
438 | }
|
439 | var editorBar = _this.editorBarComponent;
|
440 | var leftSize = moveEvent.clientX - elementPosition_1.getLeft(editorBar) - offset;
|
441 | var rightSize = editorBar.clientWidth - leftSize;
|
442 | _this.setState({ editorFlex: leftSize / rightSize });
|
443 | debounce_1.default(500, function () {
|
444 | return _this._storage.set('editorFlex', JSON.stringify(_this.state.editorFlex));
|
445 | })();
|
446 | };
|
447 | var onMouseUp = function () {
|
448 | document.removeEventListener('mousemove', onMouseMove);
|
449 | document.removeEventListener('mouseup', onMouseUp);
|
450 | onMouseMove = null;
|
451 | onMouseUp = null;
|
452 | };
|
453 | document.addEventListener('mousemove', onMouseMove);
|
454 | document.addEventListener('mouseup', onMouseUp);
|
455 | };
|
456 | _this.handleResetResize = function () {
|
457 | _this.setState({ editorFlex: 1 });
|
458 | _this._storage.set('editorFlex', JSON.stringify(_this.state.editorFlex));
|
459 | };
|
460 | _this.handleDocsResizeStart = function (downEvent) {
|
461 | downEvent.preventDefault();
|
462 | var hadWidth = _this.state.docExplorerWidth;
|
463 | var offset = downEvent.clientX - elementPosition_1.getLeft(downEvent.target);
|
464 | var onMouseMove = function (moveEvent) {
|
465 | if (moveEvent.buttons === 0) {
|
466 | return onMouseUp();
|
467 | }
|
468 | var app = _this.graphiqlContainer;
|
469 | var cursorPos = moveEvent.clientX - elementPosition_1.getLeft(app) - offset;
|
470 | var docsSize = app.clientWidth - cursorPos;
|
471 | if (docsSize < 100) {
|
472 | if (typeof _this.props.onToggleDocs === 'function') {
|
473 | _this.props.onToggleDocs(!_this.state.docExplorerOpen);
|
474 | }
|
475 | _this._storage.set('docExplorerOpen', JSON.stringify(_this.state.docExplorerOpen));
|
476 | _this.setState({ docExplorerOpen: false });
|
477 | }
|
478 | else {
|
479 | _this.setState({
|
480 | docExplorerOpen: true,
|
481 | docExplorerWidth: Math.min(docsSize, 650),
|
482 | });
|
483 | debounce_1.default(500, function () {
|
484 | return _this._storage.set('docExplorerWidth', JSON.stringify(_this.state.docExplorerWidth));
|
485 | })();
|
486 | }
|
487 | _this._storage.set('docExplorerOpen', JSON.stringify(_this.state.docExplorerOpen));
|
488 | };
|
489 | var onMouseUp = function () {
|
490 | if (!_this.state.docExplorerOpen) {
|
491 | _this.setState({ docExplorerWidth: hadWidth });
|
492 | debounce_1.default(500, function () {
|
493 | return _this._storage.set('docExplorerWidth', JSON.stringify(_this.state.docExplorerWidth));
|
494 | })();
|
495 | }
|
496 | document.removeEventListener('mousemove', onMouseMove);
|
497 | document.removeEventListener('mouseup', onMouseUp);
|
498 | onMouseMove = null;
|
499 | onMouseUp = null;
|
500 | };
|
501 | document.addEventListener('mousemove', onMouseMove);
|
502 | document.addEventListener('mouseup', onMouseUp);
|
503 | };
|
504 | _this.handleDocsResetResize = function () {
|
505 | _this.setState({
|
506 | docExplorerWidth: DEFAULT_DOC_EXPLORER_WIDTH,
|
507 | });
|
508 | debounce_1.default(500, function () {
|
509 | return _this._storage.set('docExplorerWidth', JSON.stringify(_this.state.docExplorerWidth));
|
510 | })();
|
511 | };
|
512 | _this.handleTabClickPropogation = function (downEvent) {
|
513 | downEvent.preventDefault();
|
514 | downEvent.stopPropagation();
|
515 | };
|
516 | _this.handleOpenHeaderEditorTab = function (_clickEvent) {
|
517 | _this.setState({
|
518 | headerEditorActive: true,
|
519 | variableEditorActive: false,
|
520 | secondaryEditorOpen: true,
|
521 | });
|
522 | };
|
523 | _this.handleOpenVariableEditorTab = function (_clickEvent) {
|
524 | _this.setState({
|
525 | headerEditorActive: false,
|
526 | variableEditorActive: true,
|
527 | secondaryEditorOpen: true,
|
528 | });
|
529 | };
|
530 | _this.handleSecondaryEditorResizeStart = function (downEvent) {
|
531 | downEvent.preventDefault();
|
532 | var didMove = false;
|
533 | var wasOpen = _this.state.secondaryEditorOpen;
|
534 | var hadHeight = _this.state.secondaryEditorHeight;
|
535 | var offset = downEvent.clientY - elementPosition_1.getTop(downEvent.target);
|
536 | var onMouseMove = function (moveEvent) {
|
537 | if (moveEvent.buttons === 0) {
|
538 | return onMouseUp();
|
539 | }
|
540 | didMove = true;
|
541 | var editorBar = _this.editorBarComponent;
|
542 | var topSize = moveEvent.clientY - elementPosition_1.getTop(editorBar) - offset;
|
543 | var bottomSize = editorBar.clientHeight - topSize;
|
544 | if (bottomSize < 60) {
|
545 | _this.setState({
|
546 | secondaryEditorOpen: false,
|
547 | secondaryEditorHeight: hadHeight,
|
548 | });
|
549 | }
|
550 | else {
|
551 | _this.setState({
|
552 | secondaryEditorOpen: true,
|
553 | secondaryEditorHeight: bottomSize,
|
554 | });
|
555 | }
|
556 | debounce_1.default(500, function () {
|
557 | return _this._storage.set('secondaryEditorHeight', JSON.stringify(_this.state.secondaryEditorHeight));
|
558 | })();
|
559 | };
|
560 | var onMouseUp = function () {
|
561 | if (!didMove) {
|
562 | _this.setState({ secondaryEditorOpen: !wasOpen });
|
563 | }
|
564 | document.removeEventListener('mousemove', onMouseMove);
|
565 | document.removeEventListener('mouseup', onMouseUp);
|
566 | onMouseMove = null;
|
567 | onMouseUp = null;
|
568 | };
|
569 | document.addEventListener('mousemove', onMouseMove);
|
570 | document.addEventListener('mouseup', onMouseUp);
|
571 | };
|
572 | if (typeof props.fetcher !== 'function') {
|
573 | throw new TypeError('GraphiQL requires a fetcher function.');
|
574 | }
|
575 | _this._storage = new StorageAPI_1.default(props.storage);
|
576 | _this.componentIsMounted = false;
|
577 | var query = props.query !== undefined
|
578 | ? props.query
|
579 | : _this._storage.get('query')
|
580 | ? _this._storage.get('query')
|
581 | : props.defaultQuery !== undefined
|
582 | ? props.defaultQuery
|
583 | : defaultQuery;
|
584 | var queryFacts = getQueryFacts_1.default(props.schema, query);
|
585 | var variables = props.variables !== undefined
|
586 | ? props.variables
|
587 | : _this._storage.get('variables');
|
588 | var headers = props.headers !== undefined
|
589 | ? props.headers
|
590 | : _this._storage.get('headers');
|
591 | var operationName = props.operationName !== undefined
|
592 | ? props.operationName
|
593 | : getSelectedOperationName_1.default(undefined, _this._storage.get('operationName'), queryFacts && queryFacts.operations);
|
594 | var docExplorerOpen = props.docExplorerOpen || false;
|
595 | if (_this._storage.get('docExplorerOpen')) {
|
596 | docExplorerOpen = _this._storage.get('docExplorerOpen') === 'true';
|
597 | }
|
598 | var secondaryEditorOpen;
|
599 | if (props.defaultVariableEditorOpen !== undefined) {
|
600 | secondaryEditorOpen = props.defaultVariableEditorOpen;
|
601 | }
|
602 | else if (props.defaultSecondaryEditorOpen !== undefined) {
|
603 | secondaryEditorOpen = props.defaultSecondaryEditorOpen;
|
604 | }
|
605 | else {
|
606 | secondaryEditorOpen = Boolean(variables || headers);
|
607 | }
|
608 | var headerEditorEnabled = (_a = props.headerEditorEnabled) !== null && _a !== void 0 ? _a : false;
|
609 | var shouldPersistHeaders = (_b = props.shouldPersistHeaders) !== null && _b !== void 0 ? _b : false;
|
610 | _this.state = __assign({ schema: props.schema, query: query, variables: variables, headers: headers, operationName: operationName,
|
611 | docExplorerOpen: docExplorerOpen, response: props.response, editorFlex: Number(_this._storage.get('editorFlex')) || 1, secondaryEditorOpen: secondaryEditorOpen, secondaryEditorHeight: Number(_this._storage.get('secondaryEditorHeight')) || 200, variableEditorActive: _this._storage.get('variableEditorActive') === 'true' ||
|
612 | props.headerEditorEnabled
|
613 | ? _this._storage.get('headerEditorActive') !== 'true'
|
614 | : true, headerEditorActive: _this._storage.get('headerEditorActive') === 'true', headerEditorEnabled: headerEditorEnabled,
|
615 | shouldPersistHeaders: shouldPersistHeaders, historyPaneOpen: _this._storage.get('historyPaneOpen') === 'true' || false, docExplorerWidth: Number(_this._storage.get('docExplorerWidth')) ||
|
616 | DEFAULT_DOC_EXPLORER_WIDTH, isWaitingForResponse: false, subscription: null }, queryFacts);
|
617 | return _this;
|
618 | }
|
619 | GraphiQL.formatResult = function (result) {
|
620 | return JSON.stringify(result, null, 2);
|
621 | };
|
622 | GraphiQL.formatError = function (rawError) {
|
623 | var result = Array.isArray(rawError)
|
624 | ? rawError.map(formatSingleError)
|
625 | : formatSingleError(rawError);
|
626 | return JSON.stringify(result, null, 2);
|
627 | };
|
628 | GraphiQL.prototype.componentDidMount = function () {
|
629 | this.componentIsMounted = true;
|
630 | if (this.state.schema === undefined) {
|
631 | this.fetchSchema();
|
632 | }
|
633 | this.codeMirrorSizer = new CodeMirrorSizer_1.default();
|
634 | global.g = this;
|
635 | };
|
636 | GraphiQL.prototype.UNSAFE_componentWillMount = function () {
|
637 | this.componentIsMounted = false;
|
638 | };
|
639 | GraphiQL.prototype.UNSAFE_componentWillReceiveProps = function (nextProps) {
|
640 | var _this = this;
|
641 | var nextSchema = this.state.schema;
|
642 | var nextQuery = this.state.query;
|
643 | var nextVariables = this.state.variables;
|
644 | var nextHeaders = this.state.headers;
|
645 | var nextOperationName = this.state.operationName;
|
646 | var nextResponse = this.state.response;
|
647 | if (nextProps.schema !== undefined) {
|
648 | nextSchema = nextProps.schema;
|
649 | }
|
650 | if (nextProps.query !== undefined && this.props.query !== nextProps.query) {
|
651 | nextQuery = nextProps.query;
|
652 | }
|
653 | if (nextProps.variables !== undefined &&
|
654 | this.props.variables !== nextProps.variables) {
|
655 | nextVariables = nextProps.variables;
|
656 | }
|
657 | if (nextProps.headers !== undefined &&
|
658 | this.props.headers !== nextProps.headers) {
|
659 | nextHeaders = nextProps.headers;
|
660 | }
|
661 | if (nextProps.operationName !== undefined) {
|
662 | nextOperationName = nextProps.operationName;
|
663 | }
|
664 | if (nextProps.response !== undefined) {
|
665 | nextResponse = nextProps.response;
|
666 | }
|
667 | if (nextQuery &&
|
668 | nextSchema &&
|
669 | (nextSchema !== this.state.schema ||
|
670 | nextQuery !== this.state.query ||
|
671 | nextOperationName !== this.state.operationName)) {
|
672 | var updatedQueryAttributes = this._updateQueryFacts(nextQuery, nextOperationName, this.state.operations, nextSchema);
|
673 | if (updatedQueryAttributes !== undefined) {
|
674 | nextOperationName = updatedQueryAttributes.operationName;
|
675 | this.setState(updatedQueryAttributes);
|
676 | }
|
677 | }
|
678 | if (nextProps.schema === undefined &&
|
679 | nextProps.fetcher !== this.props.fetcher) {
|
680 | nextSchema = undefined;
|
681 | }
|
682 | this._storage.set('operationName', nextOperationName);
|
683 | this.setState({
|
684 | schema: nextSchema,
|
685 | query: nextQuery,
|
686 | variables: nextVariables,
|
687 | headers: nextHeaders,
|
688 | operationName: nextOperationName,
|
689 | response: nextResponse,
|
690 | }, function () {
|
691 | if (_this.state.schema === undefined) {
|
692 | if (_this.docExplorerComponent) {
|
693 | _this.docExplorerComponent.reset();
|
694 | }
|
695 | _this.fetchSchema();
|
696 | }
|
697 | });
|
698 | };
|
699 | GraphiQL.prototype.componentDidUpdate = function () {
|
700 | this.codeMirrorSizer.updateSizes([
|
701 | this.queryEditorComponent,
|
702 | this.variableEditorComponent,
|
703 | this.headerEditorComponent,
|
704 | this.resultComponent,
|
705 | ]);
|
706 | };
|
707 | GraphiQL.prototype.render = function () {
|
708 | var _this = this;
|
709 | var _a;
|
710 | var children = react_1.default.Children.toArray(this.props.children);
|
711 | var logo = find_1.default(children, function (child) {
|
712 | return isChildComponentType(child, GraphiQL.Logo);
|
713 | }) || react_1.default.createElement(GraphiQL.Logo, null);
|
714 | var toolbar = find_1.default(children, function (child) {
|
715 | return isChildComponentType(child, GraphiQL.Toolbar);
|
716 | }) || (react_1.default.createElement(GraphiQL.Toolbar, null,
|
717 | react_1.default.createElement(ToolbarButton_1.ToolbarButton, { onClick: this.handlePrettifyQuery, title: "Prettify Query (Shift-Ctrl-P)", label: "Prettify" }),
|
718 | react_1.default.createElement(ToolbarButton_1.ToolbarButton, { onClick: this.handleMergeQuery, title: "Merge Query (Shift-Ctrl-M)", label: "Merge" }),
|
719 | react_1.default.createElement(ToolbarButton_1.ToolbarButton, { onClick: this.handleCopyQuery, title: "Copy Query (Shift-Ctrl-C)", label: "Copy" }),
|
720 | react_1.default.createElement(ToolbarButton_1.ToolbarButton, { onClick: this.handleToggleHistory, title: "Show History", label: "History" }),
|
721 | ((_a = this.props.toolbar) === null || _a === void 0 ? void 0 : _a.additionalContent) ? this.props.toolbar.additionalContent
|
722 | : null));
|
723 | var footer = find_1.default(children, function (child) {
|
724 | return isChildComponentType(child, GraphiQL.Footer);
|
725 | });
|
726 | var queryWrapStyle = {
|
727 | WebkitFlex: this.state.editorFlex,
|
728 | flex: this.state.editorFlex,
|
729 | };
|
730 | var docWrapStyle = {
|
731 | display: 'block',
|
732 | width: this.state.docExplorerWidth,
|
733 | };
|
734 | var docExplorerWrapClasses = 'docExplorerWrap' +
|
735 | (this.state.docExplorerWidth < 200 ? ' doc-explorer-narrow' : '');
|
736 | var historyPaneStyle = {
|
737 | display: this.state.historyPaneOpen ? 'block' : 'none',
|
738 | width: '230px',
|
739 | zIndex: 7,
|
740 | };
|
741 | var secondaryEditorOpen = this.state.secondaryEditorOpen;
|
742 | var secondaryEditorStyle = {
|
743 | height: secondaryEditorOpen
|
744 | ? this.state.secondaryEditorHeight
|
745 | : undefined,
|
746 | };
|
747 | return (react_1.default.createElement("div", { ref: function (n) {
|
748 | _this.graphiqlContainer = n;
|
749 | }, className: "graphiql-container" },
|
750 | this.state.historyPaneOpen && (react_1.default.createElement("div", { className: "historyPaneWrap", style: historyPaneStyle },
|
751 | react_1.default.createElement(QueryHistory_1.QueryHistory, { ref: function (node) {
|
752 | _this._queryHistory = node;
|
753 | }, operationName: this.state.operationName, query: this.state.query, variables: this.state.variables, onSelectQuery: this.handleSelectHistoryQuery, storage: this._storage, queryID: this._editorQueryID },
|
754 | react_1.default.createElement("button", { className: "docExplorerHide", onClick: this.handleToggleHistory, "aria-label": "Close History" }, '\u2715')))),
|
755 | react_1.default.createElement("div", { className: "editorWrap" },
|
756 | react_1.default.createElement("div", { className: "topBarWrap" },
|
757 | react_1.default.createElement("div", { className: "topBar" },
|
758 | logo,
|
759 | react_1.default.createElement(ExecuteButton_1.ExecuteButton, { isRunning: Boolean(this.state.subscription), onRun: this.handleRunQuery, onStop: this.handleStopQuery, operations: this.state.operations }),
|
760 | toolbar),
|
761 | !this.state.docExplorerOpen && (react_1.default.createElement("button", { className: "docExplorerShow", onClick: this.handleToggleDocs, "aria-label": "Open Documentation Explorer" }, 'Docs'))),
|
762 | react_1.default.createElement("div", { ref: function (n) {
|
763 | _this.editorBarComponent = n;
|
764 | }, className: "editorBar", onDoubleClick: this.handleResetResize, onMouseDown: this.handleResizeStart },
|
765 | react_1.default.createElement("div", { className: "queryWrap", style: queryWrapStyle },
|
766 | react_1.default.createElement(QueryEditor_1.QueryEditor, { ref: function (n) {
|
767 | _this.queryEditorComponent = n;
|
768 | }, schema: this.state.schema, validationRules: this.props.validationRules, value: this.state.query, onEdit: this.handleEditQuery, onHintInformationRender: this.handleHintInformationRender, onClickReference: this.handleClickReference, onCopyQuery: this.handleCopyQuery, onPrettifyQuery: this.handlePrettifyQuery, onMergeQuery: this.handleMergeQuery, onRunQuery: this.handleEditorRunQuery, editorTheme: this.props.editorTheme, readOnly: this.props.readOnly, externalFragments: this.props.externalFragments }),
|
769 | react_1.default.createElement("section", { className: "variable-editor secondary-editor", style: secondaryEditorStyle, "aria-label": this.state.variableEditorActive
|
770 | ? 'Query Variables'
|
771 | : 'Request Headers' },
|
772 | react_1.default.createElement("div", { className: "secondary-editor-title variable-editor-title", id: "secondary-editor-title", style: {
|
773 | cursor: secondaryEditorOpen ? 'row-resize' : 'n-resize',
|
774 | }, onMouseDown: this.handleSecondaryEditorResizeStart },
|
775 | react_1.default.createElement("div", { style: {
|
776 | cursor: 'pointer',
|
777 | color: this.state.variableEditorActive ? '#000' : 'gray',
|
778 | display: 'inline-block',
|
779 | }, onClick: this.handleOpenVariableEditorTab, onMouseDown: this.handleTabClickPropogation }, 'Query Variables'),
|
780 | this.state.headerEditorEnabled && (react_1.default.createElement("div", { style: {
|
781 | cursor: 'pointer',
|
782 | color: this.state.headerEditorActive ? '#000' : 'gray',
|
783 | display: 'inline-block',
|
784 | marginLeft: '20px',
|
785 | }, onClick: this.handleOpenHeaderEditorTab, onMouseDown: this.handleTabClickPropogation }, 'Request Headers'))),
|
786 | react_1.default.createElement(VariableEditor_1.VariableEditor, { ref: function (n) {
|
787 | _this.variableEditorComponent = n;
|
788 | }, value: this.state.variables, variableToType: this.state.variableToType, onEdit: this.handleEditVariables, onHintInformationRender: this.handleHintInformationRender, onPrettifyQuery: this.handlePrettifyQuery, onMergeQuery: this.handleMergeQuery, onRunQuery: this.handleEditorRunQuery, editorTheme: this.props.editorTheme, readOnly: this.props.readOnly, active: this.state.variableEditorActive }),
|
789 | this.state.headerEditorEnabled && (react_1.default.createElement(HeaderEditor_1.HeaderEditor, { ref: function (n) {
|
790 | _this.headerEditorComponent = n;
|
791 | }, value: this.state.headers, onEdit: this.handleEditHeaders, onHintInformationRender: this.handleHintInformationRender, onPrettifyQuery: this.handlePrettifyQuery, onMergeQuery: this.handleMergeQuery, onRunQuery: this.handleEditorRunQuery, editorTheme: this.props.editorTheme, readOnly: this.props.readOnly, active: this.state.headerEditorActive })))),
|
792 | react_1.default.createElement("div", { className: "resultWrap" },
|
793 | this.state.isWaitingForResponse && (react_1.default.createElement("div", { className: "spinner-container" },
|
794 | react_1.default.createElement("div", { className: "spinner" }))),
|
795 | react_1.default.createElement(ResultViewer_1.ResultViewer, { registerRef: function (n) {
|
796 | _this.resultViewerElement = n;
|
797 | }, ref: function (c) {
|
798 | _this.resultComponent = c;
|
799 | }, value: this.state.response, editorTheme: this.props.editorTheme, ResultsTooltip: this.props.ResultsTooltip, ImagePreview: ImagePreview_1.ImagePreview }),
|
800 | footer))),
|
801 | this.state.docExplorerOpen && (react_1.default.createElement("div", { className: docExplorerWrapClasses, style: docWrapStyle },
|
802 | react_1.default.createElement("div", { className: "docExplorerResizer", onDoubleClick: this.handleDocsResetResize, onMouseDown: this.handleDocsResizeStart }),
|
803 | react_1.default.createElement(DocExplorer_1.DocExplorer, { ref: function (c) {
|
804 | _this.docExplorerComponent = c;
|
805 | }, schema: this.state.schema },
|
806 | react_1.default.createElement("button", { className: "docExplorerHide", onClick: this.handleToggleDocs, "aria-label": "Close Documentation Explorer" }, '\u2715'))))));
|
807 | };
|
808 | GraphiQL.prototype.getQueryEditor = function () {
|
809 | if (this.queryEditorComponent) {
|
810 | return this.queryEditorComponent.getCodeMirror();
|
811 | }
|
812 | };
|
813 | GraphiQL.prototype.getVariableEditor = function () {
|
814 | if (this.variableEditorComponent) {
|
815 | return this.variableEditorComponent.getCodeMirror();
|
816 | }
|
817 | return null;
|
818 | };
|
819 | GraphiQL.prototype.getHeaderEditor = function () {
|
820 | if (this.headerEditorComponent) {
|
821 | return this.headerEditorComponent.getCodeMirror();
|
822 | }
|
823 | return null;
|
824 | };
|
825 | GraphiQL.prototype.refresh = function () {
|
826 | if (this.queryEditorComponent) {
|
827 | this.queryEditorComponent.getCodeMirror().refresh();
|
828 | }
|
829 | if (this.variableEditorComponent) {
|
830 | this.variableEditorComponent.getCodeMirror().refresh();
|
831 | }
|
832 | if (this.headerEditorComponent) {
|
833 | this.headerEditorComponent.getCodeMirror().refresh();
|
834 | }
|
835 | if (this.resultComponent) {
|
836 | this.resultComponent.getCodeMirror().refresh();
|
837 | }
|
838 | };
|
839 | GraphiQL.prototype.autoCompleteLeafs = function () {
|
840 | var _a = fillLeafs_1.fillLeafs(this.state.schema, this.state.query, this.props.getDefaultFieldNames), insertions = _a.insertions, result = _a.result;
|
841 | if (insertions && insertions.length > 0) {
|
842 | var editor_1 = this.getQueryEditor();
|
843 | if (editor_1) {
|
844 | editor_1.operation(function () {
|
845 | var cursor = editor_1.getCursor();
|
846 | var cursorIndex = editor_1.indexFromPos(cursor);
|
847 | editor_1.setValue(result || '');
|
848 | var added = 0;
|
849 | var markers = insertions.map(function (_a) {
|
850 | var index = _a.index, string = _a.string;
|
851 | return editor_1.markText(editor_1.posFromIndex(index + added), editor_1.posFromIndex(index + (added += string.length)), {
|
852 | className: 'autoInsertedLeaf',
|
853 | clearOnEnter: true,
|
854 | title: 'Automatically added leaf fields',
|
855 | });
|
856 | });
|
857 | setTimeout(function () { return markers.forEach(function (marker) { return marker.clear(); }); }, 7000);
|
858 | var newCursorIndex = cursorIndex;
|
859 | insertions.forEach(function (_a) {
|
860 | var index = _a.index, string = _a.string;
|
861 | if (index < cursorIndex) {
|
862 | newCursorIndex += string.length;
|
863 | }
|
864 | });
|
865 | editor_1.setCursor(editor_1.posFromIndex(newCursorIndex));
|
866 | });
|
867 | }
|
868 | }
|
869 | return result;
|
870 | };
|
871 | GraphiQL.prototype.fetchSchema = function () {
|
872 | var _this = this;
|
873 | var fetcher = this.props.fetcher;
|
874 | var fetcherOpts = {
|
875 | shouldPersistHeaders: Boolean(this.props.shouldPersistHeaders),
|
876 | documentAST: this.state.documentAST,
|
877 | };
|
878 | if (this.state.headers && this.state.headers.trim().length > 2) {
|
879 | fetcherOpts.headers = JSON.parse(this.state.headers);
|
880 | }
|
881 | else if (this.props.headers) {
|
882 | fetcherOpts.headers = JSON.parse(this.props.headers);
|
883 | }
|
884 | var fetch = fetcherReturnToPromise(fetcher({
|
885 | query: introspectionQueries_1.introspectionQuery,
|
886 | operationName: introspectionQueries_1.introspectionQueryName,
|
887 | }, fetcherOpts));
|
888 | if (!isPromise(fetch)) {
|
889 | this.setState({
|
890 | response: 'Fetcher did not return a Promise for introspection.',
|
891 | });
|
892 | return;
|
893 | }
|
894 | fetch
|
895 | .then(function (result) {
|
896 | if (typeof result !== 'string' && 'data' in result) {
|
897 | return result;
|
898 | }
|
899 | var fetch2 = fetcherReturnToPromise(fetcher({
|
900 | query: introspectionQueries_1.introspectionQuerySansSubscriptions,
|
901 | operationName: introspectionQueries_1.introspectionQueryName,
|
902 | }, fetcherOpts));
|
903 | if (!isPromise(fetch)) {
|
904 | throw new Error('Fetcher did not return a Promise for introspection.');
|
905 | }
|
906 | return fetch2;
|
907 | })
|
908 | .then(function (result) {
|
909 | if (_this.state.schema !== undefined) {
|
910 | return;
|
911 | }
|
912 | if (typeof result !== 'string' && 'data' in result) {
|
913 | var schema = graphql_1.buildClientSchema(result.data);
|
914 | var queryFacts = getQueryFacts_1.default(schema, _this.state.query);
|
915 | _this.safeSetState(__assign({ schema: schema }, queryFacts));
|
916 | }
|
917 | else {
|
918 | var responseString = typeof result === 'string' ? result : GraphiQL.formatResult(result);
|
919 | _this.safeSetState({
|
920 | schema: undefined,
|
921 | response: responseString,
|
922 | });
|
923 | }
|
924 | })
|
925 | .catch(function (error) {
|
926 | _this.safeSetState({
|
927 | schema: undefined,
|
928 | response: error ? GraphiQL.formatError(error) : undefined,
|
929 | });
|
930 | });
|
931 | };
|
932 | GraphiQL.prototype._fetchQuery = function (query, variables, headers, operationName, shouldPersistHeaders, cb) {
|
933 | return __awaiter(this, void 0, void 0, function () {
|
934 | var fetcher, jsonVariables, jsonHeaders, externalFragments_1, fragmentDependencies, fetch;
|
935 | var _this = this;
|
936 | return __generator(this, function (_a) {
|
937 | fetcher = this.props.fetcher;
|
938 | jsonVariables = null;
|
939 | jsonHeaders = null;
|
940 | try {
|
941 | jsonVariables =
|
942 | variables && variables.trim() !== '' ? JSON.parse(variables) : null;
|
943 | }
|
944 | catch (error) {
|
945 | throw new Error("Variables are invalid JSON: " + error.message + ".");
|
946 | }
|
947 | if (typeof jsonVariables !== 'object') {
|
948 | throw new Error('Variables are not a JSON object.');
|
949 | }
|
950 | try {
|
951 | jsonHeaders =
|
952 | headers && headers.trim() !== '' ? JSON.parse(headers) : null;
|
953 | }
|
954 | catch (error) {
|
955 | throw new Error("Headers are invalid JSON: " + error.message + ".");
|
956 | }
|
957 | if (typeof jsonHeaders !== 'object') {
|
958 | throw new Error('Headers are not a JSON object.');
|
959 | }
|
960 | if (this.props.externalFragments) {
|
961 | externalFragments_1 = new Map();
|
962 | if (Array.isArray(this.props.externalFragments)) {
|
963 | this.props.externalFragments.forEach(function (def) {
|
964 | externalFragments_1.set(def.name.value, def);
|
965 | });
|
966 | }
|
967 | else {
|
968 | graphql_1.visit(graphql_1.parse(this.props.externalFragments, {
|
969 | experimentalFragmentVariables: true,
|
970 | }), {
|
971 | FragmentDefinition: function (def) {
|
972 | externalFragments_1.set(def.name.value, def);
|
973 | },
|
974 | });
|
975 | }
|
976 | fragmentDependencies = graphql_language_service_utils_1.getFragmentDependenciesForAST(this.state.documentAST, externalFragments_1);
|
977 | if (fragmentDependencies.length > 0) {
|
978 | query +=
|
979 | '\n' +
|
980 | fragmentDependencies
|
981 | .map(function (node) { return graphql_1.print(node); })
|
982 | .join('\n');
|
983 | }
|
984 | }
|
985 | fetch = fetcher({
|
986 | query: query,
|
987 | variables: jsonVariables,
|
988 | operationName: operationName,
|
989 | }, {
|
990 | headers: jsonHeaders,
|
991 | shouldPersistHeaders: shouldPersistHeaders,
|
992 | documentAST: this.state.documentAST,
|
993 | });
|
994 | return [2, Promise.resolve(fetch)
|
995 | .then(function (value) {
|
996 | if (isObservable(value)) {
|
997 | var subscription = value.subscribe({
|
998 | next: cb,
|
999 | error: function (error) {
|
1000 | _this.safeSetState({
|
1001 | isWaitingForResponse: false,
|
1002 | response: error ? GraphiQL.formatError(error) : undefined,
|
1003 | subscription: null,
|
1004 | });
|
1005 | },
|
1006 | complete: function () {
|
1007 | _this.safeSetState({
|
1008 | isWaitingForResponse: false,
|
1009 | subscription: null,
|
1010 | });
|
1011 | },
|
1012 | });
|
1013 | return subscription;
|
1014 | }
|
1015 | else if (isAsyncIterable(value)) {
|
1016 | (function () { return __awaiter(_this, void 0, void 0, function () {
|
1017 | var value_1, value_1_1, result, e_2_1, error_2;
|
1018 | var e_2, _a;
|
1019 | return __generator(this, function (_b) {
|
1020 | switch (_b.label) {
|
1021 | case 0:
|
1022 | _b.trys.push([0, 13, , 14]);
|
1023 | _b.label = 1;
|
1024 | case 1:
|
1025 | _b.trys.push([1, 6, 7, 12]);
|
1026 | value_1 = __asyncValues(value);
|
1027 | _b.label = 2;
|
1028 | case 2: return [4, value_1.next()];
|
1029 | case 3:
|
1030 | if (!(value_1_1 = _b.sent(), !value_1_1.done)) return [3, 5];
|
1031 | result = value_1_1.value;
|
1032 | cb(result);
|
1033 | _b.label = 4;
|
1034 | case 4: return [3, 2];
|
1035 | case 5: return [3, 12];
|
1036 | case 6:
|
1037 | e_2_1 = _b.sent();
|
1038 | e_2 = { error: e_2_1 };
|
1039 | return [3, 12];
|
1040 | case 7:
|
1041 | _b.trys.push([7, , 10, 11]);
|
1042 | if (!(value_1_1 && !value_1_1.done && (_a = value_1.return))) return [3, 9];
|
1043 | return [4, _a.call(value_1)];
|
1044 | case 8:
|
1045 | _b.sent();
|
1046 | _b.label = 9;
|
1047 | case 9: return [3, 11];
|
1048 | case 10:
|
1049 | if (e_2) throw e_2.error;
|
1050 | return [7];
|
1051 | case 11: return [7];
|
1052 | case 12:
|
1053 | this.safeSetState({
|
1054 | isWaitingForResponse: false,
|
1055 | subscription: null,
|
1056 | });
|
1057 | return [3, 14];
|
1058 | case 13:
|
1059 | error_2 = _b.sent();
|
1060 | this.safeSetState({
|
1061 | isWaitingForResponse: false,
|
1062 | response: error_2 ? GraphiQL.formatError(error_2) : undefined,
|
1063 | subscription: null,
|
1064 | });
|
1065 | return [3, 14];
|
1066 | case 14: return [2];
|
1067 | }
|
1068 | });
|
1069 | }); })();
|
1070 | return {
|
1071 | unsubscribe: function () { var _a, _b; return (_b = (_a = value[Symbol.asyncIterator]()).return) === null || _b === void 0 ? void 0 : _b.call(_a); },
|
1072 | };
|
1073 | }
|
1074 | else {
|
1075 | cb(value);
|
1076 | return null;
|
1077 | }
|
1078 | })
|
1079 | .catch(function (error) {
|
1080 | _this.safeSetState({
|
1081 | isWaitingForResponse: false,
|
1082 | response: error ? GraphiQL.formatError(error) : undefined,
|
1083 | });
|
1084 | return null;
|
1085 | })];
|
1086 | });
|
1087 | });
|
1088 | };
|
1089 | GraphiQL.prototype._runQueryAtCursor = function () {
|
1090 | if (this.state.subscription) {
|
1091 | this.handleStopQuery();
|
1092 | return;
|
1093 | }
|
1094 | var operationName;
|
1095 | var operations = this.state.operations;
|
1096 | if (operations) {
|
1097 | var editor = this.getQueryEditor();
|
1098 | if (editor && editor.hasFocus()) {
|
1099 | var cursor = editor.getCursor();
|
1100 | var cursorIndex = editor.indexFromPos(cursor);
|
1101 | for (var i = 0; i < operations.length; i++) {
|
1102 | var operation = operations[i];
|
1103 | if (operation.loc &&
|
1104 | operation.loc.start <= cursorIndex &&
|
1105 | operation.loc.end >= cursorIndex) {
|
1106 | operationName = operation.name && operation.name.value;
|
1107 | break;
|
1108 | }
|
1109 | }
|
1110 | }
|
1111 | }
|
1112 | this.handleRunQuery(operationName);
|
1113 | };
|
1114 | GraphiQL.prototype._didClickDragBar = function (event) {
|
1115 | if (event.button !== 0 || event.ctrlKey) {
|
1116 | return false;
|
1117 | }
|
1118 | var target = event.target;
|
1119 | if (target.className.indexOf('CodeMirror-gutter') !== 0) {
|
1120 | return false;
|
1121 | }
|
1122 | var resultWindow = this.resultViewerElement;
|
1123 | while (target) {
|
1124 | if (target === resultWindow) {
|
1125 | return true;
|
1126 | }
|
1127 | target = target.parentNode;
|
1128 | }
|
1129 | return false;
|
1130 | };
|
1131 | GraphiQL.Logo = GraphiQLLogo;
|
1132 | GraphiQL.Toolbar = GraphiQLToolbar;
|
1133 | GraphiQL.Footer = GraphiQLFooter;
|
1134 | GraphiQL.QueryEditor = QueryEditor_1.QueryEditor;
|
1135 | GraphiQL.VariableEditor = VariableEditor_1.VariableEditor;
|
1136 | GraphiQL.HeaderEditor = HeaderEditor_1.HeaderEditor;
|
1137 | GraphiQL.ResultViewer = ResultViewer_1.ResultViewer;
|
1138 | GraphiQL.Button = ToolbarButton_1.ToolbarButton;
|
1139 | GraphiQL.ToolbarButton = ToolbarButton_1.ToolbarButton;
|
1140 | GraphiQL.Group = ToolbarGroup_1.ToolbarGroup;
|
1141 | GraphiQL.Menu = ToolbarMenu_1.ToolbarMenu;
|
1142 | GraphiQL.MenuItem = ToolbarMenu_1.ToolbarMenuItem;
|
1143 | return GraphiQL;
|
1144 | }(react_1.default.Component));
|
1145 | exports.GraphiQL = GraphiQL;
|
1146 | function GraphiQLLogo(props) {
|
1147 | return (react_1.default.createElement("div", { className: "title" }, props.children || (react_1.default.createElement("span", null,
|
1148 | 'Graph',
|
1149 | react_1.default.createElement("em", null, 'i'),
|
1150 | 'QL'))));
|
1151 | }
|
1152 | GraphiQLLogo.displayName = 'GraphiQLLogo';
|
1153 | function GraphiQLToolbar(props) {
|
1154 | return (react_1.default.createElement("div", { className: "toolbar", role: "toolbar", "aria-label": "Editor Commands" }, props.children));
|
1155 | }
|
1156 | GraphiQLToolbar.displayName = 'GraphiQLToolbar';
|
1157 | function GraphiQLFooter(props) {
|
1158 | return react_1.default.createElement("div", { className: "footer" }, props.children);
|
1159 | }
|
1160 | GraphiQLFooter.displayName = 'GraphiQLFooter';
|
1161 | var formatSingleError = function (error) { return (__assign(__assign({}, error), { message: error.message, stack: error.stack })); };
|
1162 | var defaultQuery = "# Welcome to GraphiQL\n#\n# GraphiQL is an in-browser tool for writing, validating, and\n# testing GraphQL queries.\n#\n# Type queries into this side of the screen, and you will see intelligent\n# typeaheads aware of the current GraphQL type schema and live syntax and\n# validation errors highlighted within the text.\n#\n# GraphQL queries typically start with a \"{\" character. Lines that start\n# with a # are ignored.\n#\n# An example GraphQL query might look like:\n#\n# {\n# field(arg: \"value\") {\n# subField\n# }\n# }\n#\n# Keyboard shortcuts:\n#\n# Prettify Query: Shift-Ctrl-P (or press the prettify button above)\n#\n# Merge Query: Shift-Ctrl-M (or press the merge button above)\n#\n# Run Query: Ctrl-Enter (or press the play button above)\n#\n# Auto Complete: Ctrl-Space (or just start typing)\n#\n\n";
|
1163 | function isPromise(value) {
|
1164 | return typeof value === 'object' && typeof value.then === 'function';
|
1165 | }
|
1166 | function observableToPromise(observable) {
|
1167 | return new Promise(function (resolve, reject) {
|
1168 | var subscription = observable.subscribe({
|
1169 | next: function (v) {
|
1170 | resolve(v);
|
1171 | subscription.unsubscribe();
|
1172 | },
|
1173 | error: reject,
|
1174 | complete: function () {
|
1175 | reject(new Error('no value resolved'));
|
1176 | },
|
1177 | });
|
1178 | });
|
1179 | }
|
1180 | function isObservable(value) {
|
1181 | return (typeof value === 'object' &&
|
1182 | 'subscribe' in value &&
|
1183 | typeof value.subscribe === 'function');
|
1184 | }
|
1185 | function isAsyncIterable(input) {
|
1186 | return (typeof input === 'object' &&
|
1187 | input !== null &&
|
1188 | (input[Symbol.toStringTag] === 'AsyncGenerator' ||
|
1189 | Symbol.asyncIterator in input));
|
1190 | }
|
1191 | function asyncIterableToPromise(input) {
|
1192 | return new Promise(function (resolve, reject) {
|
1193 | var _a;
|
1194 | var iteratorReturn = (_a = ('return' in input
|
1195 | ? input
|
1196 | : input[Symbol.asyncIterator]()).return) === null || _a === void 0 ? void 0 : _a.bind(input);
|
1197 | var iteratorNext = ('next' in input
|
1198 | ? input
|
1199 | : input[Symbol.asyncIterator]()).next.bind(input);
|
1200 | iteratorNext()
|
1201 | .then(function (result) {
|
1202 | resolve(result.value);
|
1203 | iteratorReturn === null || iteratorReturn === void 0 ? void 0 : iteratorReturn();
|
1204 | })
|
1205 | .catch(function (err) {
|
1206 | reject(err);
|
1207 | });
|
1208 | });
|
1209 | }
|
1210 | function fetcherReturnToPromise(fetcherResult) {
|
1211 | return Promise.resolve(fetcherResult).then(function (fetcherResult) {
|
1212 | if (isAsyncIterable(fetcherResult)) {
|
1213 | return asyncIterableToPromise(fetcherResult);
|
1214 | }
|
1215 | else if (isObservable(fetcherResult)) {
|
1216 | return observableToPromise(fetcherResult);
|
1217 | }
|
1218 | return fetcherResult;
|
1219 | });
|
1220 | }
|
1221 | function isChildComponentType(child, component) {
|
1222 | var _a;
|
1223 | if (((_a = child === null || child === void 0 ? void 0 : child.type) === null || _a === void 0 ? void 0 : _a.displayName) &&
|
1224 | child.type.displayName === component.displayName) {
|
1225 | return true;
|
1226 | }
|
1227 | return child.type === component;
|
1228 | }
|
1229 |
|
\ | No newline at end of file |