UNPKG

3.13 kBJavaScriptView Raw
1"use strict";
2var __assign = (this && this.__assign) || function () {
3 __assign = Object.assign || function(t) {
4 for (var s, i = 1, n = arguments.length; i < n; i++) {
5 s = arguments[i];
6 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7 t[p] = s[p];
8 }
9 return t;
10 };
11 return __assign.apply(this, arguments);
12};
13Object.defineProperty(exports, "__esModule", { value: true });
14var graphql_codegen_core_1 = require("graphql-codegen-core");
15var request_1 = require("request");
16var valid_url_1 = require("valid-url");
17var IntrospectionFromUrlLoader = /** @class */ (function () {
18 function IntrospectionFromUrlLoader() {
19 }
20 IntrospectionFromUrlLoader.prototype.canHandle = function (pointerToSchema) {
21 return !!valid_url_1.isUri(pointerToSchema);
22 };
23 IntrospectionFromUrlLoader.prototype.handle = function (url, cliOptions) {
24 graphql_codegen_core_1.getLogger().info("Loading GraphQL Introspection from remote: " + url + "...");
25 var splittedHeaders = (cliOptions.header || [])
26 .map(function (header) {
27 var _a;
28 var result = header.match(/^(.*?)[:=]{1}(.*?)$/);
29 if (result && result.length > 0) {
30 var name = result[1];
31 var value = result[2];
32 return _a = {},
33 _a[name] = value,
34 _a;
35 }
36 return null;
37 })
38 .filter(function (item) { return item; });
39 var extraHeaders = __assign({ Accept: 'application/json', 'Content-Type': 'application/json' }, splittedHeaders.reduce(function (prev, item) { return (__assign({}, prev, item)); }, {}));
40 graphql_codegen_core_1.debugLog("Executing POST to " + url + " with headers: ", extraHeaders);
41 return new Promise(function (resolve, reject) {
42 request_1.post({
43 url: url,
44 json: {
45 query: graphql_codegen_core_1.introspectionQuery
46 },
47 headers: extraHeaders
48 }, function (err, _response, body) {
49 if (err) {
50 reject(err);
51 return;
52 }
53 var bodyJson = body.data;
54 var errorMessage;
55 if (body.errors && body.errors.length > 0) {
56 errorMessage = body.errors.map(function (item) { return item.message; }).join(', ');
57 }
58 else if (!bodyJson) {
59 errorMessage = body;
60 }
61 if (errorMessage) {
62 reject('Unable to download schema from remote: ' + errorMessage);
63 return;
64 }
65 graphql_codegen_core_1.validateIntrospection(bodyJson);
66 resolve(graphql_codegen_core_1.introspectionToGraphQLSchema(bodyJson));
67 });
68 });
69 };
70 return IntrospectionFromUrlLoader;
71}());
72exports.IntrospectionFromUrlLoader = IntrospectionFromUrlLoader;
73//# sourceMappingURL=introspection-from-url.js.map
\No newline at end of file