1 | import { __awaiter, __generator, __read } from 'tslib';
|
2 | import { parseGraphQLSDL, parseGraphQLJSON } from '@graphql-tools/utils/es5';
|
3 | import { fetch } from 'cross-fetch';
|
4 | import { gqlPluckFromCodeString } from '@graphql-tools/graphql-tag-pluck/es5';
|
5 | import { parse } from 'graphql';
|
6 |
|
7 |
|
8 | function extractData(pointer) {
|
9 | var _a = __read(pointer.split('#'), 2), repo = _a[0], file = _a[1];
|
10 | var _b = __read(repo.split(':')[1].split('/'), 2), owner = _b[0], name = _b[1];
|
11 | var _c = __read(file.split(':'), 2), ref = _c[0], path = _c[1];
|
12 | return {
|
13 | owner: owner,
|
14 | name: name,
|
15 | ref: ref,
|
16 | path: path,
|
17 | };
|
18 | }
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | var GithubLoader = (function () {
|
30 | function GithubLoader() {
|
31 | }
|
32 | GithubLoader.prototype.canLoad = function (pointer) {
|
33 | return __awaiter(this, void 0, void 0, function () {
|
34 | return __generator(this, function (_a) {
|
35 | return [2 , typeof pointer === 'string' && pointer.toLowerCase().startsWith('github:')];
|
36 | });
|
37 | });
|
38 | };
|
39 | GithubLoader.prototype.canLoadSync = function () {
|
40 | return false;
|
41 | };
|
42 | GithubLoader.prototype.load = function (pointer, options) {
|
43 | return __awaiter(this, void 0, void 0, function () {
|
44 | var _a, owner, name, ref, path, request, response, errorMessage, content, sources;
|
45 | return __generator(this, function (_b) {
|
46 | switch (_b.label) {
|
47 | case 0: return [4 , this.canLoad(pointer)];
|
48 | case 1:
|
49 | if (!(_b.sent())) {
|
50 | return [2 , []];
|
51 | }
|
52 | _a = extractData(pointer), owner = _a.owner, name = _a.name, ref = _a.ref, path = _a.path;
|
53 | return [4 , fetch('https://api.github.com/graphql', {
|
54 | method: 'POST',
|
55 | headers: {
|
56 | 'Content-Type': 'application/json; charset=utf-8',
|
57 | Authorization: "bearer " + options.token,
|
58 | },
|
59 | body: JSON.stringify({
|
60 | query: "\n query GetGraphQLSchemaForGraphQLtools($owner: String!, $name: String!, $expression: String!) {\n repository(owner: $owner, name: $name) {\n object(expression: $expression) {\n ... on Blob {\n text\n }\n }\n }\n }\n ",
|
61 | variables: {
|
62 | owner: owner,
|
63 | name: name,
|
64 | expression: ref + ':' + path,
|
65 | },
|
66 | operationName: 'GetGraphQLSchemaForGraphQLtools',
|
67 | }),
|
68 | })];
|
69 | case 2:
|
70 | request = _b.sent();
|
71 | return [4 , request.json()];
|
72 | case 3:
|
73 | response = _b.sent();
|
74 | errorMessage = null;
|
75 | if (response.errors && response.errors.length > 0) {
|
76 | errorMessage = response.errors.map(function (item) { return item.message; }).join(', ');
|
77 | }
|
78 | else if (!response.data) {
|
79 | errorMessage = response;
|
80 | }
|
81 | if (errorMessage) {
|
82 | throw new Error('Unable to download schema from github: ' + errorMessage);
|
83 | }
|
84 | content = response.data.repository.object.text;
|
85 | if (/\.(gql|graphql)s?$/i.test(path)) {
|
86 | return [2 , [parseGraphQLSDL(pointer, content, options)]];
|
87 | }
|
88 | if (/\.json$/i.test(path)) {
|
89 | return [2 , [parseGraphQLJSON(pointer, content, options)]];
|
90 | }
|
91 | if (!(path.endsWith('.tsx') || path.endsWith('.ts') || path.endsWith('.js') || path.endsWith('.jsx'))) return [3 , 5];
|
92 | return [4 , gqlPluckFromCodeString(pointer, content, options.pluckConfig)];
|
93 | case 4:
|
94 | sources = _b.sent();
|
95 | return [2 , sources.map(function (source) { return ({
|
96 | location: pointer,
|
97 | document: parse(source, options),
|
98 | }); })];
|
99 | case 5: throw new Error("Invalid file extension: " + path);
|
100 | }
|
101 | });
|
102 | });
|
103 | };
|
104 | GithubLoader.prototype.loadSync = function () {
|
105 | throw new Error('Loader GitHub has no sync mode');
|
106 | };
|
107 | return GithubLoader;
|
108 | }());
|
109 |
|
110 | export { GithubLoader };
|