UNPKG

2.85 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, config, schemaOptions) {
24 // spinner.info(`Loading GraphQL Introspection from remote: ${url}...`);
25 var headers = {};
26 if (Array.isArray(schemaOptions.headers)) {
27 headers = schemaOptions.headers.reduce(function (prev, v) { return (__assign({}, prev, v)); }, {});
28 }
29 else if (typeof schemaOptions.headers === 'object') {
30 headers = schemaOptions.headers;
31 }
32 var extraHeaders = __assign({ Accept: 'application/json', 'Content-Type': 'application/json' }, headers);
33 graphql_codegen_core_1.debugLog("Executing POST to " + url + " with headers: ", extraHeaders);
34 return new Promise(function (resolve, reject) {
35 request_1.post({
36 url: url,
37 json: {
38 query: graphql_codegen_core_1.introspectionQuery
39 },
40 headers: extraHeaders
41 }, function (err, _response, body) {
42 if (err) {
43 reject(err);
44 return;
45 }
46 var bodyJson = body.data;
47 var errorMessage;
48 if (body.errors && body.errors.length > 0) {
49 errorMessage = body.errors.map(function (item) { return item.message; }).join(', ');
50 }
51 else if (!bodyJson) {
52 errorMessage = body;
53 }
54 if (errorMessage) {
55 reject('Unable to download schema from remote: ' + errorMessage);
56 return;
57 }
58 graphql_codegen_core_1.validateIntrospection(bodyJson);
59 resolve(graphql_codegen_core_1.introspectionToGraphQLSchema(bodyJson));
60 });
61 });
62 };
63 return IntrospectionFromUrlLoader;
64}());
65exports.IntrospectionFromUrlLoader = IntrospectionFromUrlLoader;
66//# sourceMappingURL=introspection-from-url.js.map
\No newline at end of file