UNPKG

2.08 kBJavaScriptView Raw
1"use strict";
2var __assign = (this && this.__assign) || Object.assign || function(t) {
3 for (var s, i = 1, n = arguments.length; i < n; i++) {
4 s = arguments[i];
5 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6 t[p] = s[p];
7 }
8 return t;
9};
10Object.defineProperty(exports, "__esModule", { value: true });
11var graphql_codegen_core_1 = require("graphql-codegen-core");
12var request = require("request");
13exports.introspectionFromUrl = function (url, headers) {
14 console.log("Loading GraphQL Introspection from remote: " + url + "...");
15 var splittedHeaders = (headers || []).map(function (header) {
16 var result = header.match(/^(.*?)[:=]{1}(.*?)$/);
17 if (result && result.length > 0) {
18 var name = result[1];
19 var value = result[2];
20 return _a = {},
21 _a[name] = value,
22 _a;
23 }
24 return null;
25 var _a;
26 }).filter(function (item) { return item; });
27 var extraHeaders = __assign({ 'Accept': 'application/json', 'Content-Type': 'application/json' }, (splittedHeaders.reduce(function (prev, item) { return (__assign({}, prev, item)); }, {})));
28 graphql_codegen_core_1.debugLog("Executing POST to " + url + " with headers: ", extraHeaders);
29 return new Promise(function (resolve, reject) {
30 request.post({
31 url: url,
32 json: {
33 query: graphql_codegen_core_1.introspectionQuery.replace('locations', '')
34 },
35 headers: extraHeaders,
36 }, function (err, response, body) {
37 if (err) {
38 reject(err);
39 return;
40 }
41 var bodyJson = body.data;
42 if (!bodyJson || (body.errors && body.errors.length > 0)) {
43 reject('Unable to download schema from remote: ' + body.errors.map(function (item) { return item.message; }).join(', '));
44 return;
45 }
46 resolve(bodyJson);
47 });
48 });
49};
50//# sourceMappingURL=introspection-from-url.js.map
\No newline at end of file