UNPKG

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