Skip to content

Commit ecfe6a6

Browse files
ardatann1ru4l
andauthored
fix(load): load from all loaders at once even if one returns some result (#3184)
* Try to load afrom all loaders * TypeScript :) * add test Co-authored-by: Laurin Quast <[email protected]>
1 parent 74581cf commit ecfe6a6

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

packages/load/src/load-typedefs/load-file.ts

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,47 @@
1-
import { Source, Maybe, isSome } from '@graphql-tools/utils';
1+
import { Source } from '@graphql-tools/utils';
22
import { env } from 'process';
33
import { LoadTypedefsOptions } from '../load-typedefs';
44

5-
export async function loadFile(pointer: string, options: LoadTypedefsOptions): Promise<Maybe<Source[]>> {
5+
export async function loadFile(pointer: string, options: LoadTypedefsOptions): Promise<Source[]> {
66
const cached = useCache({ pointer, options });
77

88
if (cached) {
99
return cached;
1010
}
1111

12-
for await (const loader of options.loaders) {
13-
try {
14-
const loadedValue = await loader.load(pointer, options);
15-
if (!isSome(loadedValue) || loadedValue.length === 0) {
16-
continue;
17-
}
18-
return loadedValue;
19-
} catch (error) {
20-
if (env['DEBUG']) {
21-
console.error(`Failed to find any GraphQL type definitions in: ${pointer} - ${error.message}`);
12+
const results: Source[] = [];
13+
14+
await Promise.all(
15+
options.loaders.map(async loader => {
16+
try {
17+
const loaderResults = await loader.load(pointer, options);
18+
loaderResults?.forEach(result => results.push(result));
19+
} catch (error) {
20+
if (env['DEBUG']) {
21+
console.error(`Failed to find any GraphQL type definitions in: ${pointer} - ${error.message}`);
22+
}
23+
throw error;
2224
}
23-
throw error;
24-
}
25-
}
25+
})
26+
);
2627

27-
return undefined;
28+
return results;
2829
}
2930

30-
export function loadFileSync(pointer: string, options: LoadTypedefsOptions): Maybe<Source[]> {
31+
export function loadFileSync(pointer: string, options: LoadTypedefsOptions): Source[] {
3132
const cached = useCache({ pointer, options });
3233

3334
if (cached) {
3435
return cached;
3536
}
3637

38+
const results: Source[] = [];
39+
3740
for (const loader of options.loaders) {
3841
try {
3942
// We check for the existence so it is okay to force non null
40-
const loadedValue = loader.loadSync!(pointer, options);
41-
if (!isSome(loadedValue) || loadedValue.length === 0) {
42-
continue;
43-
}
44-
return loadedValue;
43+
const loaderResults = loader.loadSync!(pointer, options);
44+
loaderResults?.forEach(result => results.push(result));
4545
} catch (error) {
4646
if (env['DEBUG']) {
4747
console.error(`Failed to find any GraphQL type definitions in: ${pointer} - ${error.message}`);
@@ -50,7 +50,7 @@ export function loadFileSync(pointer: string, options: LoadTypedefsOptions): May
5050
}
5151
}
5252

53-
return undefined;
53+
return results;
5454
}
5555

5656
function useCache<T extends any>({ pointer, options }: { pointer: string; options: T }) {

packages/load/tests/loaders/documents/documents-from-glob.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,5 +140,14 @@ describe('documentsFromGlob', () => {
140140
});
141141
expect(result.length).toBe(1);
142142
})
143+
test(`should try loading using all loaders`, async () => {
144+
const glob = join(__dirname, './test-files/', '(tags.js|2.graphql)');
145+
const result = await load(glob, {
146+
loaders: [new GraphQLFileLoader(), new CodeFileLoader()],
147+
});
148+
// 1 from 2.graphql
149+
// 2 from tags.js
150+
expect(result.length).toEqual(3);
151+
})
143152
})
144153
});

0 commit comments

Comments
 (0)