@graphql-tools/github-loader 7.3.3 → 7.3.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/index.js +115 -0
- package/package.json +3 -3
package/cjs/index.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GithubLoader = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const utils_1 = require("@graphql-tools/utils");
|
|
6
|
+
const graphql_tag_pluck_1 = require("@graphql-tools/graphql-tag-pluck");
|
|
7
|
+
const graphql_1 = require("graphql");
|
|
8
|
+
const sync_fetch_1 = tslib_1.__importDefault(require("sync-fetch"));
|
|
9
|
+
const fetch_1 = require("@whatwg-node/fetch");
|
|
10
|
+
// github:owner/name#ref:path/to/file
|
|
11
|
+
function extractData(pointer) {
|
|
12
|
+
const [repo, file] = pointer.split('#');
|
|
13
|
+
const [owner, name] = repo.split(':')[1].split('/');
|
|
14
|
+
const [ref, path] = file.split(':');
|
|
15
|
+
return {
|
|
16
|
+
owner,
|
|
17
|
+
name,
|
|
18
|
+
ref,
|
|
19
|
+
path,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* This loader loads a file from GitHub.
|
|
24
|
+
*
|
|
25
|
+
* ```js
|
|
26
|
+
* const typeDefs = await loadTypedefs('github:githubUser/githubRepo#branchName:path/to/file.ts', {
|
|
27
|
+
* loaders: [new GithubLoader()],
|
|
28
|
+
* token: YOUR_GITHUB_TOKEN,
|
|
29
|
+
* })
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
class GithubLoader {
|
|
33
|
+
async canLoad(pointer) {
|
|
34
|
+
return this.canLoadSync(pointer);
|
|
35
|
+
}
|
|
36
|
+
canLoadSync(pointer) {
|
|
37
|
+
return typeof pointer === 'string' && pointer.toLowerCase().startsWith('github:');
|
|
38
|
+
}
|
|
39
|
+
async load(pointer, options) {
|
|
40
|
+
if (!(await this.canLoad(pointer))) {
|
|
41
|
+
return [];
|
|
42
|
+
}
|
|
43
|
+
const { owner, name, ref, path } = extractData(pointer);
|
|
44
|
+
const fetch = options.customFetch || fetch_1.fetch;
|
|
45
|
+
const request = await fetch('https://api.github.com/graphql', this.prepareRequest({ owner, ref, path, name, options }));
|
|
46
|
+
const response = await request.json();
|
|
47
|
+
return this.handleResponse({ pointer, path, options, response });
|
|
48
|
+
}
|
|
49
|
+
loadSync(pointer, options) {
|
|
50
|
+
if (!this.canLoadSync(pointer)) {
|
|
51
|
+
return [];
|
|
52
|
+
}
|
|
53
|
+
const { owner, name, ref, path } = extractData(pointer);
|
|
54
|
+
const fetch = options.customFetch || sync_fetch_1.default;
|
|
55
|
+
const request = fetch('https://api.github.com/graphql', this.prepareRequest({ owner, ref, path, name, options }));
|
|
56
|
+
const response = request.json();
|
|
57
|
+
return this.handleResponse({ pointer, path, options, response });
|
|
58
|
+
}
|
|
59
|
+
handleResponse({ pointer, path, options, response }) {
|
|
60
|
+
let errorMessage = null;
|
|
61
|
+
if (response.errors && response.errors.length > 0) {
|
|
62
|
+
errorMessage = response.errors.map((item) => item.message).join(', ');
|
|
63
|
+
}
|
|
64
|
+
else if (!response.data) {
|
|
65
|
+
errorMessage = response;
|
|
66
|
+
}
|
|
67
|
+
if (errorMessage) {
|
|
68
|
+
throw new Error('Unable to download schema from github: ' + errorMessage);
|
|
69
|
+
}
|
|
70
|
+
const content = response.data.repository.object.text;
|
|
71
|
+
if (/\.(gql|graphql)s?$/i.test(path)) {
|
|
72
|
+
return [(0, utils_1.parseGraphQLSDL)(pointer, content, options)];
|
|
73
|
+
}
|
|
74
|
+
if (/\.json$/i.test(path)) {
|
|
75
|
+
return [(0, utils_1.parseGraphQLJSON)(pointer, content, options)];
|
|
76
|
+
}
|
|
77
|
+
if (path.endsWith('.tsx') || path.endsWith('.ts') || path.endsWith('.js') || path.endsWith('.jsx')) {
|
|
78
|
+
const sources = (0, graphql_tag_pluck_1.gqlPluckFromCodeStringSync)(pointer, content, options.pluckConfig);
|
|
79
|
+
return sources.map(source => ({
|
|
80
|
+
location: pointer,
|
|
81
|
+
document: (0, graphql_1.parse)(source, options),
|
|
82
|
+
}));
|
|
83
|
+
}
|
|
84
|
+
throw new Error(`Invalid file extension: ${path}`);
|
|
85
|
+
}
|
|
86
|
+
prepareRequest({ owner, ref, path, name, options, }) {
|
|
87
|
+
return {
|
|
88
|
+
method: 'POST',
|
|
89
|
+
headers: {
|
|
90
|
+
'content-type': 'application/json; charset=utf-8',
|
|
91
|
+
authorization: `bearer ${options.token}`,
|
|
92
|
+
},
|
|
93
|
+
body: JSON.stringify({
|
|
94
|
+
query: `
|
|
95
|
+
query GetGraphQLSchemaForGraphQLtools($owner: String!, $name: String!, $expression: String!) {
|
|
96
|
+
repository(owner: $owner, name: $name) {
|
|
97
|
+
object(expression: $expression) {
|
|
98
|
+
... on Blob {
|
|
99
|
+
text
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
`,
|
|
105
|
+
variables: {
|
|
106
|
+
owner,
|
|
107
|
+
name,
|
|
108
|
+
expression: ref + ':' + path,
|
|
109
|
+
},
|
|
110
|
+
operationName: 'GetGraphQLSchemaForGraphQLtools',
|
|
111
|
+
}),
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
exports.GithubLoader = GithubLoader;
|
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphql-tools/github-loader",
|
|
3
|
-
"version": "7.3.
|
|
3
|
+
"version": "7.3.5",
|
|
4
4
|
"description": "A set of utils for faster development of GraphQL tools",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"peerDependencies": {
|
|
7
7
|
"graphql": "^14.0.0 || ^15.0.0 || ^16.0.0 || ^17.0.0"
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|
|
10
|
-
"@graphql-tools/utils": "8.
|
|
11
|
-
"@graphql-tools/graphql-tag-pluck": "7.3.
|
|
10
|
+
"@graphql-tools/utils": "8.9.0",
|
|
11
|
+
"@graphql-tools/graphql-tag-pluck": "7.3.1",
|
|
12
12
|
"@whatwg-node/fetch": "^0.0.2",
|
|
13
13
|
"sync-fetch": "0.4.1",
|
|
14
14
|
"tslib": "^2.4.0"
|