@graphql-tools/github-loader 7.3.2-alpha-b93d3b57.0 → 7.3.4
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/esm/index.js +110 -0
- package/package.json +1 -1
- package/typings/index.d.ts +50 -0
package/esm/index.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { parseGraphQLSDL, parseGraphQLJSON } from '@graphql-tools/utils';
|
|
2
|
+
import { gqlPluckFromCodeStringSync } from '@graphql-tools/graphql-tag-pluck';
|
|
3
|
+
import { parse } from 'graphql';
|
|
4
|
+
import syncFetch from 'sync-fetch';
|
|
5
|
+
import { fetch as asyncFetch } from '@whatwg-node/fetch';
|
|
6
|
+
// github:owner/name#ref:path/to/file
|
|
7
|
+
function extractData(pointer) {
|
|
8
|
+
const [repo, file] = pointer.split('#');
|
|
9
|
+
const [owner, name] = repo.split(':')[1].split('/');
|
|
10
|
+
const [ref, path] = file.split(':');
|
|
11
|
+
return {
|
|
12
|
+
owner,
|
|
13
|
+
name,
|
|
14
|
+
ref,
|
|
15
|
+
path,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* This loader loads a file from GitHub.
|
|
20
|
+
*
|
|
21
|
+
* ```js
|
|
22
|
+
* const typeDefs = await loadTypedefs('github:githubUser/githubRepo#branchName:path/to/file.ts', {
|
|
23
|
+
* loaders: [new GithubLoader()],
|
|
24
|
+
* token: YOUR_GITHUB_TOKEN,
|
|
25
|
+
* })
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export class GithubLoader {
|
|
29
|
+
async canLoad(pointer) {
|
|
30
|
+
return this.canLoadSync(pointer);
|
|
31
|
+
}
|
|
32
|
+
canLoadSync(pointer) {
|
|
33
|
+
return typeof pointer === 'string' && pointer.toLowerCase().startsWith('github:');
|
|
34
|
+
}
|
|
35
|
+
async load(pointer, options) {
|
|
36
|
+
if (!(await this.canLoad(pointer))) {
|
|
37
|
+
return [];
|
|
38
|
+
}
|
|
39
|
+
const { owner, name, ref, path } = extractData(pointer);
|
|
40
|
+
const fetch = options.customFetch || asyncFetch;
|
|
41
|
+
const request = await fetch('https://api.github.com/graphql', this.prepareRequest({ owner, ref, path, name, options }));
|
|
42
|
+
const response = await request.json();
|
|
43
|
+
return this.handleResponse({ pointer, path, options, response });
|
|
44
|
+
}
|
|
45
|
+
loadSync(pointer, options) {
|
|
46
|
+
if (!this.canLoadSync(pointer)) {
|
|
47
|
+
return [];
|
|
48
|
+
}
|
|
49
|
+
const { owner, name, ref, path } = extractData(pointer);
|
|
50
|
+
const fetch = options.customFetch || syncFetch;
|
|
51
|
+
const request = fetch('https://api.github.com/graphql', this.prepareRequest({ owner, ref, path, name, options }));
|
|
52
|
+
const response = request.json();
|
|
53
|
+
return this.handleResponse({ pointer, path, options, response });
|
|
54
|
+
}
|
|
55
|
+
handleResponse({ pointer, path, options, response }) {
|
|
56
|
+
let errorMessage = null;
|
|
57
|
+
if (response.errors && response.errors.length > 0) {
|
|
58
|
+
errorMessage = response.errors.map((item) => item.message).join(', ');
|
|
59
|
+
}
|
|
60
|
+
else if (!response.data) {
|
|
61
|
+
errorMessage = response;
|
|
62
|
+
}
|
|
63
|
+
if (errorMessage) {
|
|
64
|
+
throw new Error('Unable to download schema from github: ' + errorMessage);
|
|
65
|
+
}
|
|
66
|
+
const content = response.data.repository.object.text;
|
|
67
|
+
if (/\.(gql|graphql)s?$/i.test(path)) {
|
|
68
|
+
return [parseGraphQLSDL(pointer, content, options)];
|
|
69
|
+
}
|
|
70
|
+
if (/\.json$/i.test(path)) {
|
|
71
|
+
return [parseGraphQLJSON(pointer, content, options)];
|
|
72
|
+
}
|
|
73
|
+
if (path.endsWith('.tsx') || path.endsWith('.ts') || path.endsWith('.js') || path.endsWith('.jsx')) {
|
|
74
|
+
const sources = gqlPluckFromCodeStringSync(pointer, content, options.pluckConfig);
|
|
75
|
+
return sources.map(source => ({
|
|
76
|
+
location: pointer,
|
|
77
|
+
document: parse(source, options),
|
|
78
|
+
}));
|
|
79
|
+
}
|
|
80
|
+
throw new Error(`Invalid file extension: ${path}`);
|
|
81
|
+
}
|
|
82
|
+
prepareRequest({ owner, ref, path, name, options, }) {
|
|
83
|
+
return {
|
|
84
|
+
method: 'POST',
|
|
85
|
+
headers: {
|
|
86
|
+
'content-type': 'application/json; charset=utf-8',
|
|
87
|
+
authorization: `bearer ${options.token}`,
|
|
88
|
+
},
|
|
89
|
+
body: JSON.stringify({
|
|
90
|
+
query: `
|
|
91
|
+
query GetGraphQLSchemaForGraphQLtools($owner: String!, $name: String!, $expression: String!) {
|
|
92
|
+
repository(owner: $owner, name: $name) {
|
|
93
|
+
object(expression: $expression) {
|
|
94
|
+
... on Blob {
|
|
95
|
+
text
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
`,
|
|
101
|
+
variables: {
|
|
102
|
+
owner,
|
|
103
|
+
name,
|
|
104
|
+
expression: ref + ':' + path,
|
|
105
|
+
},
|
|
106
|
+
operationName: 'GetGraphQLSchemaForGraphQLtools',
|
|
107
|
+
}),
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
}
|
package/package.json
CHANGED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { Loader, BaseLoaderOptions, Source } from '@graphql-tools/utils';
|
|
2
|
+
import { GraphQLTagPluckOptions } from '@graphql-tools/graphql-tag-pluck';
|
|
3
|
+
import syncFetch from 'sync-fetch';
|
|
4
|
+
import { fetch as asyncFetch } from '@whatwg-node/fetch';
|
|
5
|
+
/**
|
|
6
|
+
* Additional options for loading from GitHub
|
|
7
|
+
*/
|
|
8
|
+
export interface GithubLoaderOptions extends BaseLoaderOptions {
|
|
9
|
+
/**
|
|
10
|
+
* A GitHub access token
|
|
11
|
+
*/
|
|
12
|
+
token: string;
|
|
13
|
+
/**
|
|
14
|
+
* Additional options to pass to `graphql-tag-pluck`
|
|
15
|
+
*/
|
|
16
|
+
pluckConfig?: GraphQLTagPluckOptions;
|
|
17
|
+
customFetch?: typeof asyncFetch | typeof syncFetch;
|
|
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
|
+
*/
|
|
29
|
+
export declare class GithubLoader implements Loader<GithubLoaderOptions> {
|
|
30
|
+
canLoad(pointer: string): Promise<boolean>;
|
|
31
|
+
canLoadSync(pointer: string): boolean;
|
|
32
|
+
load(pointer: string, options: GithubLoaderOptions): Promise<Source[]>;
|
|
33
|
+
loadSync(pointer: string, options: GithubLoaderOptions): Source[];
|
|
34
|
+
handleResponse({ pointer, path, options, response }: {
|
|
35
|
+
pointer: string;
|
|
36
|
+
path: string;
|
|
37
|
+
options: any;
|
|
38
|
+
response: any;
|
|
39
|
+
}): Source[] | {
|
|
40
|
+
location: string | undefined;
|
|
41
|
+
document: import("graphql").DocumentNode;
|
|
42
|
+
}[];
|
|
43
|
+
prepareRequest({ owner, ref, path, name, options, }: {
|
|
44
|
+
owner: string;
|
|
45
|
+
ref: string;
|
|
46
|
+
path: string;
|
|
47
|
+
name: string;
|
|
48
|
+
options: GithubLoaderOptions;
|
|
49
|
+
}): RequestInit;
|
|
50
|
+
}
|