Skip to content

Commit d3fe8d8

Browse files
authored
fix(load): respect filters in DEBUG env var (#5715)
1 parent 3863a82 commit d3fe8d8

File tree

8 files changed

+87
-148
lines changed

8 files changed

+87
-148
lines changed

.changeset/purple-owls-nail.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@graphql-tools/utils': patch
3+
'@graphql-tools/load': patch
4+
---
5+
6+
Print debug timer logs by respecting the filters in DEBUG env var

packages/load/src/load-typedefs.ts

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
import { env } from 'process';
2-
import { asArray, BaseLoaderOptions, compareStrings, Loader, Source } from '@graphql-tools/utils';
1+
import {
2+
asArray,
3+
BaseLoaderOptions,
4+
compareStrings,
5+
debugTimerEnd,
6+
debugTimerStart,
7+
Loader,
8+
Source,
9+
} from '@graphql-tools/utils';
310
import { collectSources, collectSourcesSync } from './load-typedefs/collect-sources.js';
411
import { applyDefaultOptions } from './load-typedefs/options.js';
512
import { parseSource } from './load-typedefs/parse.js';
@@ -30,9 +37,7 @@ export async function loadTypedefs<AdditionalConfig = Record<string, unknown>>(
3037
pointerOrPointers: UnnormalizedTypeDefPointer | UnnormalizedTypeDefPointer[],
3138
options: LoadTypedefsOptions<Partial<AdditionalConfig>>,
3239
): Promise<Source[]> {
33-
if (env['DEBUG'] != null) {
34-
console.time('@graphql-tools/load: loadTypedefs');
35-
}
40+
debugTimerStart('@graphql-tools/load: loadTypedefs');
3641
const { ignore, pointerOptionMap } = normalizePointers(pointerOrPointers);
3742

3843
options.ignore = asArray(options.ignore || []);
@@ -67,9 +72,7 @@ export async function loadTypedefs<AdditionalConfig = Record<string, unknown>>(
6772

6873
const result = prepareResult({ options, pointerOptionMap, validSources });
6974

70-
if (env['DEBUG'] != null) {
71-
console.timeEnd('@graphql-tools/load: loadTypedefs');
72-
}
75+
debugTimerEnd('@graphql-tools/load: loadTypedefs');
7376

7477
return result;
7578
}
@@ -85,9 +88,7 @@ export function loadTypedefsSync<AdditionalConfig = Record<string, unknown>>(
8588
pointerOrPointers: UnnormalizedTypeDefPointer | UnnormalizedTypeDefPointer[],
8689
options: LoadTypedefsOptions<Partial<AdditionalConfig>>,
8790
): Source[] {
88-
if (env['DEBUG'] != null) {
89-
console.time('@graphql-tools/load: loadTypedefsSync');
90-
}
91+
debugTimerStart('@graphql-tools/load: loadTypedefsSync');
9192
const { ignore, pointerOptionMap } = normalizePointers(pointerOrPointers);
9293

9394
options.ignore = asArray(options.ignore || []).concat(ignore);
@@ -114,9 +115,7 @@ export function loadTypedefsSync<AdditionalConfig = Record<string, unknown>>(
114115

115116
const result = prepareResult({ options, pointerOptionMap, validSources });
116117

117-
if (env['DEBUG'] != null) {
118-
console.timeEnd('@graphql-tools/load: loadTypedefsSync');
119-
}
118+
debugTimerEnd('@graphql-tools/load: loadTypedefsSync');
120119

121120
return result;
122121
}
@@ -132,9 +131,7 @@ function prepareResult({
132131
pointerOptionMap: any;
133132
validSources: Source[];
134133
}) {
135-
if (env['DEBUG'] != null) {
136-
console.time('@graphql-tools/load: prepareResult');
137-
}
134+
debugTimerStart('@graphql-tools/load: prepareResult');
138135
const pointerList = Object.keys(pointerOptionMap);
139136

140137
if (pointerList.length > 0 && validSources.length === 0) {
@@ -150,8 +147,7 @@ function prepareResult({
150147
const sortedResult = options.sort
151148
? validSources.sort((left, right) => compareStrings(left.location, right.location))
152149
: validSources;
153-
if (env['DEBUG'] != null) {
154-
console.timeEnd('@graphql-tools/load: prepareResult');
155-
}
150+
151+
debugTimerEnd('@graphql-tools/load: prepareResult');
156152
return sortedResult;
157153
}

packages/load/src/load-typedefs/collect-sources.ts

Lines changed: 25 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { createRequire } from 'module';
2-
import { cwd, env } from 'process';
2+
import { cwd } from 'process';
33
import { isSchema, Kind } from 'graphql';
44
import {
55
asArray,
6+
debugTimerEnd,
7+
debugTimerStart,
68
getDocumentNodeFromSchema,
79
isDocumentString,
810
parseGraphQLSDL,
@@ -28,9 +30,7 @@ export async function collectSources<TOptions>({
2830
};
2931
options: LoadTypedefsOptions<Partial<TOptions>>;
3032
}): Promise<Source[]> {
31-
if (env['DEBUG'] != null) {
32-
console.time('@graphql-tools/load: collectSources');
33-
}
33+
debugTimerStart('@graphql-tools/load: collectSources');
3434
const sources: Source[] = [];
3535
const queue = useQueue<void>({ concurrency: CONCURRENCY_LIMIT });
3636

@@ -42,9 +42,7 @@ export async function collectSources<TOptions>({
4242
for (const pointer in pointerOptionMap) {
4343
const pointerOptions = pointerOptionMap[pointer];
4444

45-
if (env['DEBUG'] != null) {
46-
console.time(`@graphql-tools/load: collectSources ${pointer}`);
47-
}
45+
debugTimerStart(`@graphql-tools/load: collectSources ${pointer}`);
4846
collect({
4947
pointer,
5048
pointerOptions,
@@ -53,18 +51,12 @@ export async function collectSources<TOptions>({
5351
addSource,
5452
queue: queue.add as AddToQueue<void>,
5553
});
56-
if (env['DEBUG'] != null) {
57-
console.timeEnd(`@graphql-tools/load: collectSources ${pointer}`);
58-
}
54+
debugTimerEnd(`@graphql-tools/load: collectSources ${pointer}`);
5955
}
6056

61-
if (env['DEBUG'] != null) {
62-
console.time('@graphql-tools/load: collectSources queue');
63-
}
57+
debugTimerStart('@graphql-tools/load: collectSources queue');
6458
await queue.runAll();
65-
if (env['DEBUG'] != null) {
66-
console.timeEnd('@graphql-tools/load: collectSources queue');
67-
}
59+
debugTimerEnd('@graphql-tools/load: collectSources queue');
6860
return sources;
6961
}
7062

@@ -85,16 +77,12 @@ export function collectSourcesSync<TOptions>({
8577
stack: [collectDocumentString, collectCustomLoaderSync, collectFallbackSync],
8678
});
8779

88-
if (env['DEBUG'] != null) {
89-
console.time('@graphql-tools/load: collectSourcesSync');
90-
}
80+
debugTimerStart('@graphql-tools/load: collectSourcesSync');
9181

9282
for (const pointer in pointerOptionMap) {
9383
const pointerOptions = pointerOptionMap[pointer];
9484

95-
if (env['DEBUG'] != null) {
96-
console.time(`@graphql-tools/load: collectSourcesSync ${pointer}`);
97-
}
85+
debugTimerStart(`@graphql-tools/load: collectSourcesSync ${pointer}`);
9886
collect({
9987
pointer,
10088
pointerOptions,
@@ -103,19 +91,13 @@ export function collectSourcesSync<TOptions>({
10391
addSource,
10492
queue: queue.add,
10593
});
106-
if (env['DEBUG'] != null) {
107-
console.timeEnd(`@graphql-tools/load: collectSourcesSync ${pointer}`);
108-
}
94+
debugTimerEnd(`@graphql-tools/load: collectSourcesSync ${pointer}`);
10995
}
11096

111-
if (env['DEBUG'] != null) {
112-
console.time('@graphql-tools/load: collectSourcesSync queue');
113-
}
97+
debugTimerStart('@graphql-tools/load: collectSourcesSync queue');
11498
queue.runAll();
99+
debugTimerEnd('@graphql-tools/load: collectSourcesSync queue');
115100

116-
if (env['DEBUG'] != null) {
117-
console.timeEnd('@graphql-tools/load: collectSourcesSync queue');
118-
}
119101
return sources;
120102
}
121103

@@ -156,9 +138,7 @@ function addResultOfCustomLoader({
156138
result: any;
157139
addSource: AddSource;
158140
}) {
159-
if (env['DEBUG'] != null) {
160-
console.time(`@graphql-tools/load: addResultOfCustomLoader ${pointer}`);
161-
}
141+
debugTimerStart(`@graphql-tools/load: addResultOfCustomLoader ${pointer}`);
162142
if (isSchema(result)) {
163143
addSource({
164144
source: {
@@ -186,18 +166,14 @@ function addResultOfCustomLoader({
186166
pointer,
187167
});
188168
}
189-
if (env['DEBUG'] != null) {
190-
console.timeEnd(`@graphql-tools/load: addResultOfCustomLoader ${pointer}`);
191-
}
169+
debugTimerEnd(`@graphql-tools/load: addResultOfCustomLoader ${pointer}`);
192170
}
193171

194172
function collectDocumentString<T>(
195173
{ pointer, pointerOptions, options, addSource, queue }: CollectOptions<T>,
196174
next: StackNext,
197175
) {
198-
if (env['DEBUG'] != null) {
199-
console.time(`@graphql-tools/load: collectDocumentString ${pointer}`);
200-
}
176+
debugTimerStart(`@graphql-tools/load: collectDocumentString ${pointer}`);
201177
if (isDocumentString(pointer)) {
202178
return queue(() => {
203179
const source = parseGraphQLSDL(`${stringToHash(pointer)}.graphql`, pointer, {
@@ -211,9 +187,7 @@ function collectDocumentString<T>(
211187
});
212188
});
213189
}
214-
if (env['DEBUG'] != null) {
215-
console.timeEnd(`@graphql-tools/load: collectDocumentString ${pointer}`);
216-
}
190+
debugTimerEnd(`@graphql-tools/load: collectDocumentString ${pointer}`);
217191

218192
next();
219193
}
@@ -224,18 +198,14 @@ function collectCustomLoader<T>(
224198
) {
225199
if (pointerOptions.loader) {
226200
return queue(async () => {
227-
if (env['DEBUG'] != null) {
228-
console.time(`@graphql-tools/load: collectCustomLoader ${pointer}`);
229-
}
201+
debugTimerStart(`@graphql-tools/load: collectCustomLoader ${pointer}`);
230202
await Promise.all(asArray(pointerOptions.require).map(m => import(m)));
231203
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
232204
// @ts-ignore TODO options.cwd is possibly undefined, but it seems like no test covers this path
233205
const loader = await useCustomLoader(pointerOptions.loader, options.cwd);
234206
const result = await loader(pointer, { ...options, ...pointerOptions }, pointerOptionMap);
235207

236-
if (env['DEBUG'] != null) {
237-
console.timeEnd(`@graphql-tools/load: collectCustomLoader ${pointer}`);
238-
}
208+
debugTimerEnd(`@graphql-tools/load: collectCustomLoader ${pointer}`);
239209
if (!result) {
240210
return;
241211
}
@@ -253,9 +223,7 @@ function collectCustomLoaderSync<T>(
253223
) {
254224
if (pointerOptions.loader) {
255225
return queue(() => {
256-
if (env['DEBUG'] != null) {
257-
console.time(`@graphql-tools/load: collectCustomLoaderSync ${pointer}`);
258-
}
226+
debugTimerStart(`@graphql-tools/load: collectCustomLoaderSync ${pointer}`);
259227
const cwdRequire = createRequire(options.cwd || cwd());
260228
for (const m of asArray(pointerOptions.require)) {
261229
cwdRequire(m);
@@ -265,9 +233,7 @@ function collectCustomLoaderSync<T>(
265233
const loader = useCustomLoaderSync(pointerOptions.loader, options.cwd);
266234
const result = loader(pointer, { ...options, ...pointerOptions }, pointerOptionMap);
267235

268-
if (env['DEBUG'] != null) {
269-
console.timeEnd(`@graphql-tools/load: collectCustomLoaderSync ${pointer}`);
270-
}
236+
debugTimerEnd(`@graphql-tools/load: collectCustomLoaderSync ${pointer}`);
271237
if (result) {
272238
addResultOfCustomLoader({ pointer, result, addSource });
273239
}
@@ -285,9 +251,7 @@ function collectFallback<T>({
285251
addSource,
286252
}: CollectOptions<T>) {
287253
return queue(async () => {
288-
if (env['DEBUG'] != null) {
289-
console.time(`@graphql-tools/load: collectFallback ${pointer}`);
290-
}
254+
debugTimerStart(`@graphql-tools/load: collectFallback ${pointer}`);
291255
const sources = await loadFile(pointer, {
292256
...options,
293257
...pointerOptions,
@@ -298,9 +262,7 @@ function collectFallback<T>({
298262
addSource({ source, pointer });
299263
}
300264
}
301-
if (env['DEBUG'] != null) {
302-
console.timeEnd(`@graphql-tools/load: collectFallback ${pointer}`);
303-
}
265+
debugTimerEnd(`@graphql-tools/load: collectFallback ${pointer}`);
304266
});
305267
}
306268

@@ -312,9 +274,7 @@ function collectFallbackSync<T>({
312274
addSource,
313275
}: CollectOptions<T>) {
314276
return queue(() => {
315-
if (env['DEBUG'] != null) {
316-
console.time(`@graphql-tools/load: collectFallbackSync ${pointer}`);
317-
}
277+
debugTimerStart(`@graphql-tools/load: collectFallbackSync ${pointer}`);
318278
const sources = loadFileSync(pointer, {
319279
...options,
320280
...pointerOptions,
@@ -325,8 +285,6 @@ function collectFallbackSync<T>({
325285
addSource({ source, pointer });
326286
}
327287
}
328-
if (env['DEBUG'] != null) {
329-
console.timeEnd(`@graphql-tools/load: collectFallbackSync ${pointer}`);
330-
}
288+
debugTimerEnd(`@graphql-tools/load: collectFallbackSync ${pointer}`);
331289
});
332290
}

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

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import { env } from 'process';
2-
import { Source } from '@graphql-tools/utils';
2+
import { debugTimerEnd, debugTimerStart, Source } from '@graphql-tools/utils';
33
import { LoadTypedefsOptions } from '../load-typedefs.js';
44

55
export async function loadFile(pointer: string, options: LoadTypedefsOptions): Promise<Source[]> {
6-
if (env['DEBUG'] != null) {
7-
console.time(`@graphql-tools/load: loadFile ${pointer}`);
8-
}
6+
debugTimerStart(`@graphql-tools/load: loadFile ${pointer}`);
97
let results = options.cache?.[pointer];
108

119
if (!results) {
@@ -47,17 +45,13 @@ export async function loadFile(pointer: string, options: LoadTypedefsOptions): P
4745
}
4846
}
4947

50-
if (env['DEBUG'] != null) {
51-
console.timeEnd(`@graphql-tools/load: loadFile ${pointer}`);
52-
}
48+
debugTimerEnd(`@graphql-tools/load: loadFile ${pointer}`);
5349

5450
return results;
5551
}
5652

5753
export function loadFileSync(pointer: string, options: LoadTypedefsOptions): Source[] {
58-
if (env['DEBUG'] != null) {
59-
console.time(`@graphql-tools/load: loadFileSync ${pointer}`);
60-
}
54+
debugTimerStart(`@graphql-tools/load: loadFileSync ${pointer}`);
6155
let results = options.cache?.[pointer];
6256

6357
if (!results) {
@@ -99,9 +93,7 @@ export function loadFileSync(pointer: string, options: LoadTypedefsOptions): Sou
9993
}
10094
}
10195

102-
if (env['DEBUG'] != null) {
103-
console.timeEnd(`@graphql-tools/load: loadFileSync ${pointer}`);
104-
}
96+
debugTimerEnd(`@graphql-tools/load: loadFileSync ${pointer}`);
10597

10698
return results;
10799
}

0 commit comments

Comments
 (0)