-
-
Notifications
You must be signed in to change notification settings - Fork 796
/
load-typedefs.ts
116 lines (96 loc) · 3.05 KB
/
load-typedefs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { Source, SingleFileOptions, Loader, compareStrings } from '@graphql-tools/utils';
import { normalizePointers } from './utils/pointers';
import { applyDefaultOptions } from './load-typedefs/options';
import { collectSources, collectSourcesSync } from './load-typedefs/collect-sources';
import { parseSource } from './load-typedefs/parse';
import { useLimit } from './utils/helpers';
const CONCURRENCY_LIMIT = 100;
export type LoadTypedefsOptions<ExtraConfig = { [key: string]: any }> = SingleFileOptions &
ExtraConfig & {
cache?: { [key: string]: Source };
loaders: Loader[];
filterKinds?: string[];
ignore?: string | string[];
sort?: boolean;
};
export type UnnormalizedTypeDefPointer = { [key: string]: any } | string;
export async function loadTypedefs<AdditionalConfig = Record<string, unknown>>(
pointerOrPointers: UnnormalizedTypeDefPointer | UnnormalizedTypeDefPointer[],
options: LoadTypedefsOptions<Partial<AdditionalConfig>>
): Promise<Source[]> {
const pointerOptionMap = normalizePointers(pointerOrPointers);
const globOptions: any = {};
applyDefaultOptions<AdditionalConfig>(options);
const sources = await collectSources({
pointerOptionMap,
options,
});
const validSources: Source[] = [];
// If we have few k of files it may be an issue
const limit = useLimit(CONCURRENCY_LIMIT);
await Promise.all(
sources.map(partialSource =>
limit(() =>
parseSource({
partialSource,
options,
globOptions,
pointerOptionMap,
addValidSource(source) {
validSources.push(source);
},
})
)
)
);
return prepareResult({ options, pointerOptionMap, validSources });
}
export function loadTypedefsSync<AdditionalConfig = Record<string, unknown>>(
pointerOrPointers: UnnormalizedTypeDefPointer | UnnormalizedTypeDefPointer[],
options: LoadTypedefsOptions<Partial<AdditionalConfig>>
): Source[] {
const pointerOptionMap = normalizePointers(pointerOrPointers);
const globOptions: any = {};
applyDefaultOptions<AdditionalConfig>(options);
const sources = collectSourcesSync({
pointerOptionMap,
options,
});
const validSources: Source[] = [];
sources.forEach(partialSource => {
parseSource({
partialSource,
options,
globOptions,
pointerOptionMap,
addValidSource(source) {
validSources.push(source);
},
});
});
return prepareResult({ options, pointerOptionMap, validSources });
}
//
function prepareResult({
options,
pointerOptionMap,
validSources,
}: {
options: any;
pointerOptionMap: any;
validSources: Source[];
}) {
const pointerList = Object.keys(pointerOptionMap);
if (pointerList.length > 0 && validSources.length === 0) {
throw new Error(`
Unable to find any GraphQL type definitions for the following pointers:
${pointerList.map(
p => `
- ${p}
`
)}`);
}
return options.sort
? validSources.sort((left, right) => compareStrings(left.location, right.location))
: validSources;
}