-
-
Notifications
You must be signed in to change notification settings - Fork 824
/
Copy pathload-typedefs.ts
131 lines (109 loc) · 3.83 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { Source, BaseLoaderOptions, Loader, compareStrings, asArray } 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 }> = BaseLoaderOptions &
ExtraConfig & {
cache?: { [key: string]: Source[] };
loaders: Loader[];
filterKinds?: string[];
sort?: boolean;
};
export type UnnormalizedTypeDefPointer = { [key: string]: any } | string;
/**
* Asynchronously loads any GraphQL documents (i.e. executable documents like
* operations and fragments as well as type system definitions) from the
* provided pointers.
* loadTypedefs does not merge the typeDefs when `#import` is used ( https://github.com/ardatan/graphql-tools/issues/2980#issuecomment-1003692728 )
* @param pointerOrPointers Pointers to the sources to load the documents from
* @param options Additional options
*/
export async function loadTypedefs<AdditionalConfig = Record<string, unknown>>(
pointerOrPointers: UnnormalizedTypeDefPointer | UnnormalizedTypeDefPointer[],
options: LoadTypedefsOptions<Partial<AdditionalConfig>>
): Promise<Source[]> {
const { ignore, pointerOptionMap } = normalizePointers(pointerOrPointers);
options.ignore = asArray(options.ignore || []);
options.ignore.push(...ignore);
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,
pointerOptionMap,
addValidSource(source) {
validSources.push(source);
},
})
)
)
);
return prepareResult({ options, pointerOptionMap, validSources });
}
/**
* Synchronously loads any GraphQL documents (i.e. executable documents like
* operations and fragments as well as type system definitions) from the
* provided pointers.
* @param pointerOrPointers Pointers to the sources to load the documents from
* @param options Additional options
*/
export function loadTypedefsSync<AdditionalConfig = Record<string, unknown>>(
pointerOrPointers: UnnormalizedTypeDefPointer | UnnormalizedTypeDefPointer[],
options: LoadTypedefsOptions<Partial<AdditionalConfig>>
): Source[] {
const { ignore, pointerOptionMap } = normalizePointers(pointerOrPointers);
options.ignore = asArray(options.ignore || []).concat(ignore);
applyDefaultOptions<AdditionalConfig>(options);
const sources = collectSourcesSync({
pointerOptionMap,
options,
});
const validSources: Source[] = [];
for (const partialSource of sources) {
parseSource({
partialSource,
options,
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;
}