UNPKG

3.3 kBJavaScriptView Raw
1import { parseGraphQLSDL, parseGraphQLJSON } from '@graphql-tools/utils';
2import { fetch } from 'cross-fetch';
3import { gqlPluckFromCodeString } from '@graphql-tools/graphql-tag-pluck';
4import { parse } from 'graphql';
5
6// github:owner/name#ref:path/to/file
7function 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 */
28class GithubLoader {
29 async canLoad(pointer) {
30 return typeof pointer === 'string' && pointer.toLowerCase().startsWith('github:');
31 }
32 canLoadSync() {
33 return false;
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 request = await fetch('https://api.github.com/graphql', {
41 method: 'POST',
42 headers: {
43 'Content-Type': 'application/json; charset=utf-8',
44 Authorization: `bearer ${options.token}`,
45 },
46 body: JSON.stringify({
47 query: `
48 query GetGraphQLSchemaForGraphQLtools($owner: String!, $name: String!, $expression: String!) {
49 repository(owner: $owner, name: $name) {
50 object(expression: $expression) {
51 ... on Blob {
52 text
53 }
54 }
55 }
56 }
57 `,
58 variables: {
59 owner,
60 name,
61 expression: ref + ':' + path,
62 },
63 operationName: 'GetGraphQLSchemaForGraphQLtools',
64 }),
65 });
66 const response = await request.json();
67 let errorMessage = null;
68 if (response.errors && response.errors.length > 0) {
69 errorMessage = response.errors.map((item) => item.message).join(', ');
70 }
71 else if (!response.data) {
72 errorMessage = response;
73 }
74 if (errorMessage) {
75 throw new Error('Unable to download schema from github: ' + errorMessage);
76 }
77 const content = response.data.repository.object.text;
78 if (/\.(gql|graphql)s?$/i.test(path)) {
79 return [parseGraphQLSDL(pointer, content, options)];
80 }
81 if (/\.json$/i.test(path)) {
82 return [parseGraphQLJSON(pointer, content, options)];
83 }
84 if (path.endsWith('.tsx') || path.endsWith('.ts') || path.endsWith('.js') || path.endsWith('.jsx')) {
85 const sources = await gqlPluckFromCodeString(pointer, content, options.pluckConfig);
86 return sources.map(source => ({
87 location: pointer,
88 document: parse(source, options),
89 }));
90 }
91 throw new Error(`Invalid file extension: ${path}`);
92 }
93 loadSync() {
94 throw new Error('Loader GitHub has no sync mode');
95 }
96}
97
98export { GithubLoader };