UNPKG

5.39 kBJavaScriptView Raw
1import { __awaiter, __generator, __read } from 'tslib';
2import { parseGraphQLSDL, parseGraphQLJSON } from '@graphql-tools/utils/es5';
3import { fetch } from 'cross-fetch';
4import { gqlPluckFromCodeString } from '@graphql-tools/graphql-tag-pluck/es5';
5import { parse } from 'graphql';
6
7// github:owner/name#ref:path/to/file
8function 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 * This loader loads a file from GitHub.
21 *
22 * ```js
23 * const typeDefs = await loadTypedefs('github:githubUser/githubRepo#branchName:path/to/file.ts', {
24 * loaders: [new GithubLoader()],
25 * token: YOUR_GITHUB_TOKEN,
26 * })
27 * ```
28 */
29var GithubLoader = /** @class */ (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 /*return*/, 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 /*yield*/, this.canLoad(pointer)];
48 case 1:
49 if (!(_b.sent())) {
50 return [2 /*return*/, []];
51 }
52 _a = extractData(pointer), owner = _a.owner, name = _a.name, ref = _a.ref, path = _a.path;
53 return [4 /*yield*/, 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 /*yield*/, 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 /*return*/, [parseGraphQLSDL(pointer, content, options)]];
87 }
88 if (/\.json$/i.test(path)) {
89 return [2 /*return*/, [parseGraphQLJSON(pointer, content, options)]];
90 }
91 if (!(path.endsWith('.tsx') || path.endsWith('.ts') || path.endsWith('.js') || path.endsWith('.jsx'))) return [3 /*break*/, 5];
92 return [4 /*yield*/, gqlPluckFromCodeString(pointer, content, options.pluckConfig)];
93 case 4:
94 sources = _b.sent();
95 return [2 /*return*/, 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
110export { GithubLoader };