Skip to content

Commit e9fa613

Browse files
authored
fix: git loader should simply ignore non git paths instead of panicking (#3188)
1 parent ecfe6a6 commit e9fa613

File tree

2 files changed

+83
-34
lines changed

2 files changed

+83
-34
lines changed

packages/loaders/git/src/index.ts

Lines changed: 79 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ import { asArray, BaseLoaderOptions, Loader, Source } from '@graphql-tools/utils
1313
import isGlob from 'is-glob';
1414

1515
// git:branch:path/to/file
16-
function extractData(pointer: string): {
16+
function extractData(pointer: string): null | {
1717
ref: string;
1818
path: string;
1919
} {
2020
const parts = pointer.replace(/^git\:/i, '').split(':');
2121

2222
if (!parts || parts.length !== 2) {
23-
throw new Error('Schema pointer should match "git:branchName:path/to/file"');
23+
return null;
2424
}
2525

2626
return {
@@ -61,17 +61,24 @@ export class GitLoader implements Loader<GitLoaderOptions> {
6161
return typeof pointer === 'string' && pointer.toLowerCase().startsWith('git:');
6262
}
6363

64-
async resolveGlobs(glob: string, ignores: string[]) {
64+
async resolveGlobs(glob: string, ignores: string[]): Promise<Array<string>> {
65+
const data = extractData(glob);
66+
if (data === null) {
67+
return [];
68+
}
6569
const refsForPaths = new Map();
66-
67-
const { ref, path } = extractData(glob);
70+
const { ref, path } = data;
6871
if (!refsForPaths.has(ref)) {
6972
refsForPaths.set(ref, []);
7073
}
7174
refsForPaths.get(ref).push(unixify(path));
7275

7376
for (const ignore of ignores) {
74-
const { ref, path } = extractData(ignore);
77+
const data = extractData(ignore);
78+
if (data === null) {
79+
continue;
80+
}
81+
const { ref, path } = data;
7582
if (!refsForPaths.has(ref)) {
7683
refsForPaths.set(ref, []);
7784
}
@@ -88,16 +95,24 @@ export class GitLoader implements Loader<GitLoaderOptions> {
8895
}
8996

9097
resolveGlobsSync(glob: string, ignores: string[]) {
98+
const data = extractData(glob);
99+
if (data === null) {
100+
return [];
101+
}
102+
const { ref, path } = data;
91103
const refsForPaths = new Map();
92104

93-
const { ref, path } = extractData(glob);
94105
if (!refsForPaths.has(ref)) {
95106
refsForPaths.set(ref, []);
96107
}
97108
refsForPaths.get(ref).push(unixify(path));
98109

99110
for (const ignore of ignores) {
100-
const { ref, path } = extractData(ignore);
111+
const data = extractData(ignore);
112+
if (data === null) {
113+
continue;
114+
}
115+
const { ref, path } = data;
101116
if (!refsForPaths.has(ref)) {
102117
refsForPaths.set(ref, []);
103118
}
@@ -111,19 +126,12 @@ export class GitLoader implements Loader<GitLoaderOptions> {
111126
return resolved;
112127
}
113128

114-
async load(pointer: string, options: GitLoaderOptions): Promise<Source[]> {
115-
const { ref, path } = extractData(pointer);
116-
if (isGlob(path)) {
117-
const resolvedPaths = await this.resolveGlobs(pointer, asArray(options.ignore || []));
118-
const finalResult: Source[] = [];
119-
120-
await Promise.all(
121-
resolvedPaths.map(async path => {
122-
const results = await this.load(path, options);
123-
results?.forEach(result => finalResult.push(result));
124-
})
125-
);
129+
private async _load(pointer: string, options: GitLoaderOptions): Promise<Source[]> {
130+
const result = extractData(pointer);
131+
if (result === null) {
132+
return [];
126133
}
134+
const { ref, path } = result;
127135
const content = await loadFromGit({ ref, path });
128136
const parsed = handleStuff({ path, options, pointer, content });
129137

@@ -139,17 +147,37 @@ export class GitLoader implements Loader<GitLoaderOptions> {
139147
}));
140148
}
141149

142-
loadSync(pointer: string, options: GitLoaderOptions): Source[] {
143-
const { ref, path } = extractData(pointer);
150+
async load(pointer: string, options: GitLoaderOptions): Promise<Source[]> {
151+
const result = extractData(pointer);
152+
if (result === null) {
153+
return [];
154+
}
155+
const { path } = result;
156+
const finalResult: Source[] = [];
157+
144158
if (isGlob(path)) {
145-
const resolvedPaths = this.resolveGlobsSync(pointer, asArray(options.ignore || []));
146-
const finalResult: Source[] = [];
159+
const resolvedPaths = await this.resolveGlobs(pointer, asArray(options.ignore || []));
147160

148-
resolvedPaths.forEach(path => {
149-
const results = this.loadSync(path, options);
150-
results?.forEach(result => finalResult.push(result));
151-
});
161+
await Promise.all(
162+
resolvedPaths.map(async path => {
163+
const results = await this.load(path, options);
164+
finalResult.push(...results);
165+
})
166+
);
167+
} else if (await this.canLoad(pointer)) {
168+
finalResult.push(...(await this._load(pointer, options)));
152169
}
170+
171+
return finalResult;
172+
}
173+
174+
private _loadSync(pointer: string, options: GitLoaderOptions): Source[] {
175+
const result = extractData(pointer);
176+
if (result === null) {
177+
return [];
178+
}
179+
const { ref, path } = result;
180+
153181
const content = loadFromGitSync({ ref, path });
154182
const parsed = handleStuff({ path, options, pointer, content });
155183

@@ -164,4 +192,27 @@ export class GitLoader implements Loader<GitLoaderOptions> {
164192
document: parse(source, options),
165193
}));
166194
}
195+
196+
loadSync(pointer: string, options: GitLoaderOptions): Source[] {
197+
const result = extractData(pointer);
198+
if (result === null) {
199+
return [];
200+
}
201+
const { path } = result;
202+
const finalResult: Source[] = [];
203+
204+
if (isGlob(path)) {
205+
const resolvedPaths = this.resolveGlobsSync(pointer, asArray(options.ignore || []));
206+
const finalResult: Source[] = [];
207+
for (const path of resolvedPaths) {
208+
if (this.canLoadSync(path)) {
209+
finalResult.push(...this.loadSync(path, options));
210+
}
211+
}
212+
} else if (this.canLoadSync(pointer)) {
213+
finalResult.push(...this._loadSync(pointer, options));
214+
}
215+
216+
return finalResult;
217+
}
167218
}

packages/loaders/git/tests/loader.spec.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,15 @@ describe('GitLoader', () => {
6060
expect(result.document).toMatchSnapshot();
6161
});
6262

63-
it('should throw when pointer is malformed', async () => {
64-
await expect(load(getPointer('foo:graphql'), {})).rejects.toThrowError(
65-
'Schema pointer should match "git:branchName:path/to/file"'
66-
);
67-
});
68-
6963
it('should throw when the file does not exist', async () => {
7064
await expect(load(getPointer('wrong-filename.graphql'), {})).rejects.toThrowError(
7165
'Unable to load file from git'
7266
);
7367
});
68+
it('should simply ignore a non git path', async () => {
69+
const result = await load('./pluckable.ts', {});
70+
expect(result).toEqual([])
71+
})
7472
});
7573
});
7674
});

0 commit comments

Comments
 (0)