From 7b1f3276a35fd676fe902b637d276545bc015c0b Mon Sep 17 00:00:00 2001 From: Arda TANRIKULU Date: Thu, 23 Nov 2023 16:28:17 +0300 Subject: [PATCH] fix(load): respect filters in DEBUG env var --- .changeset/purple-owls-nail.md | 6 ++ packages/load/src/load-typedefs.ts | 36 ++++---- .../load/src/load-typedefs/collect-sources.ts | 92 +++++-------------- packages/load/src/load-typedefs/load-file.ts | 18 +--- packages/load/src/load-typedefs/parse.ts | 48 +++------- packages/load/src/utils/pointers.ts | 19 +--- packages/utils/src/debugTimer.ts | 15 +++ packages/utils/src/index.ts | 1 + 8 files changed, 87 insertions(+), 148 deletions(-) create mode 100644 .changeset/purple-owls-nail.md create mode 100644 packages/utils/src/debugTimer.ts diff --git a/.changeset/purple-owls-nail.md b/.changeset/purple-owls-nail.md new file mode 100644 index 00000000000..e91f6b1e3a9 --- /dev/null +++ b/.changeset/purple-owls-nail.md @@ -0,0 +1,6 @@ +--- +'@graphql-tools/utils': patch +'@graphql-tools/load': patch +--- + +Print debug timer logs by respecting the filters in DEBUG env var diff --git a/packages/load/src/load-typedefs.ts b/packages/load/src/load-typedefs.ts index e15a854edf7..3d03f75fe43 100644 --- a/packages/load/src/load-typedefs.ts +++ b/packages/load/src/load-typedefs.ts @@ -1,5 +1,12 @@ -import { env } from 'process'; -import { asArray, BaseLoaderOptions, compareStrings, Loader, Source } from '@graphql-tools/utils'; +import { + asArray, + BaseLoaderOptions, + compareStrings, + debugTimerEnd, + debugTimerStart, + Loader, + Source, +} from '@graphql-tools/utils'; import { collectSources, collectSourcesSync } from './load-typedefs/collect-sources.js'; import { applyDefaultOptions } from './load-typedefs/options.js'; import { parseSource } from './load-typedefs/parse.js'; @@ -30,9 +37,7 @@ export async function loadTypedefs>( pointerOrPointers: UnnormalizedTypeDefPointer | UnnormalizedTypeDefPointer[], options: LoadTypedefsOptions>, ): Promise { - if (env['DEBUG'] != null) { - console.time('@graphql-tools/load: loadTypedefs'); - } + debugTimerStart('@graphql-tools/load: loadTypedefs'); const { ignore, pointerOptionMap } = normalizePointers(pointerOrPointers); options.ignore = asArray(options.ignore || []); @@ -67,9 +72,7 @@ export async function loadTypedefs>( const result = prepareResult({ options, pointerOptionMap, validSources }); - if (env['DEBUG'] != null) { - console.timeEnd('@graphql-tools/load: loadTypedefs'); - } + debugTimerEnd('@graphql-tools/load: loadTypedefs'); return result; } @@ -85,9 +88,7 @@ export function loadTypedefsSync>( pointerOrPointers: UnnormalizedTypeDefPointer | UnnormalizedTypeDefPointer[], options: LoadTypedefsOptions>, ): Source[] { - if (env['DEBUG'] != null) { - console.time('@graphql-tools/load: loadTypedefsSync'); - } + debugTimerStart('@graphql-tools/load: loadTypedefsSync'); const { ignore, pointerOptionMap } = normalizePointers(pointerOrPointers); options.ignore = asArray(options.ignore || []).concat(ignore); @@ -114,9 +115,7 @@ export function loadTypedefsSync>( const result = prepareResult({ options, pointerOptionMap, validSources }); - if (env['DEBUG'] != null) { - console.timeEnd('@graphql-tools/load: loadTypedefsSync'); - } + debugTimerEnd('@graphql-tools/load: loadTypedefsSync'); return result; } @@ -132,9 +131,7 @@ function prepareResult({ pointerOptionMap: any; validSources: Source[]; }) { - if (env['DEBUG'] != null) { - console.time('@graphql-tools/load: prepareResult'); - } + debugTimerStart('@graphql-tools/load: prepareResult'); const pointerList = Object.keys(pointerOptionMap); if (pointerList.length > 0 && validSources.length === 0) { @@ -150,8 +147,7 @@ function prepareResult({ const sortedResult = options.sort ? validSources.sort((left, right) => compareStrings(left.location, right.location)) : validSources; - if (env['DEBUG'] != null) { - console.timeEnd('@graphql-tools/load: prepareResult'); - } + + debugTimerEnd('@graphql-tools/load: prepareResult'); return sortedResult; } diff --git a/packages/load/src/load-typedefs/collect-sources.ts b/packages/load/src/load-typedefs/collect-sources.ts index aad45ec3fe9..3af2a0a2874 100644 --- a/packages/load/src/load-typedefs/collect-sources.ts +++ b/packages/load/src/load-typedefs/collect-sources.ts @@ -1,8 +1,10 @@ import { createRequire } from 'module'; -import { cwd, env } from 'process'; +import { cwd } from 'process'; import { isSchema, Kind } from 'graphql'; import { asArray, + debugTimerEnd, + debugTimerStart, getDocumentNodeFromSchema, isDocumentString, parseGraphQLSDL, @@ -28,9 +30,7 @@ export async function collectSources({ }; options: LoadTypedefsOptions>; }): Promise { - if (env['DEBUG'] != null) { - console.time('@graphql-tools/load: collectSources'); - } + debugTimerStart('@graphql-tools/load: collectSources'); const sources: Source[] = []; const queue = useQueue({ concurrency: CONCURRENCY_LIMIT }); @@ -42,9 +42,7 @@ export async function collectSources({ for (const pointer in pointerOptionMap) { const pointerOptions = pointerOptionMap[pointer]; - if (env['DEBUG'] != null) { - console.time(`@graphql-tools/load: collectSources ${pointer}`); - } + debugTimerStart(`@graphql-tools/load: collectSources ${pointer}`); collect({ pointer, pointerOptions, @@ -53,18 +51,12 @@ export async function collectSources({ addSource, queue: queue.add as AddToQueue, }); - if (env['DEBUG'] != null) { - console.timeEnd(`@graphql-tools/load: collectSources ${pointer}`); - } + debugTimerEnd(`@graphql-tools/load: collectSources ${pointer}`); } - if (env['DEBUG'] != null) { - console.time('@graphql-tools/load: collectSources queue'); - } + debugTimerStart('@graphql-tools/load: collectSources queue'); await queue.runAll(); - if (env['DEBUG'] != null) { - console.timeEnd('@graphql-tools/load: collectSources queue'); - } + debugTimerEnd('@graphql-tools/load: collectSources queue'); return sources; } @@ -85,16 +77,12 @@ export function collectSourcesSync({ stack: [collectDocumentString, collectCustomLoaderSync, collectFallbackSync], }); - if (env['DEBUG'] != null) { - console.time('@graphql-tools/load: collectSourcesSync'); - } + debugTimerStart('@graphql-tools/load: collectSourcesSync'); for (const pointer in pointerOptionMap) { const pointerOptions = pointerOptionMap[pointer]; - if (env['DEBUG'] != null) { - console.time(`@graphql-tools/load: collectSourcesSync ${pointer}`); - } + debugTimerStart(`@graphql-tools/load: collectSourcesSync ${pointer}`); collect({ pointer, pointerOptions, @@ -103,19 +91,13 @@ export function collectSourcesSync({ addSource, queue: queue.add, }); - if (env['DEBUG'] != null) { - console.timeEnd(`@graphql-tools/load: collectSourcesSync ${pointer}`); - } + debugTimerEnd(`@graphql-tools/load: collectSourcesSync ${pointer}`); } - if (env['DEBUG'] != null) { - console.time('@graphql-tools/load: collectSourcesSync queue'); - } + debugTimerStart('@graphql-tools/load: collectSourcesSync queue'); queue.runAll(); + debugTimerEnd('@graphql-tools/load: collectSourcesSync queue'); - if (env['DEBUG'] != null) { - console.timeEnd('@graphql-tools/load: collectSourcesSync queue'); - } return sources; } @@ -156,9 +138,7 @@ function addResultOfCustomLoader({ result: any; addSource: AddSource; }) { - if (env['DEBUG'] != null) { - console.time(`@graphql-tools/load: addResultOfCustomLoader ${pointer}`); - } + debugTimerStart(`@graphql-tools/load: addResultOfCustomLoader ${pointer}`); if (isSchema(result)) { addSource({ source: { @@ -186,18 +166,14 @@ function addResultOfCustomLoader({ pointer, }); } - if (env['DEBUG'] != null) { - console.timeEnd(`@graphql-tools/load: addResultOfCustomLoader ${pointer}`); - } + debugTimerEnd(`@graphql-tools/load: addResultOfCustomLoader ${pointer}`); } function collectDocumentString( { pointer, pointerOptions, options, addSource, queue }: CollectOptions, next: StackNext, ) { - if (env['DEBUG'] != null) { - console.time(`@graphql-tools/load: collectDocumentString ${pointer}`); - } + debugTimerStart(`@graphql-tools/load: collectDocumentString ${pointer}`); if (isDocumentString(pointer)) { return queue(() => { const source = parseGraphQLSDL(`${stringToHash(pointer)}.graphql`, pointer, { @@ -211,9 +187,7 @@ function collectDocumentString( }); }); } - if (env['DEBUG'] != null) { - console.timeEnd(`@graphql-tools/load: collectDocumentString ${pointer}`); - } + debugTimerEnd(`@graphql-tools/load: collectDocumentString ${pointer}`); next(); } @@ -224,18 +198,14 @@ function collectCustomLoader( ) { if (pointerOptions.loader) { return queue(async () => { - if (env['DEBUG'] != null) { - console.time(`@graphql-tools/load: collectCustomLoader ${pointer}`); - } + debugTimerStart(`@graphql-tools/load: collectCustomLoader ${pointer}`); await Promise.all(asArray(pointerOptions.require).map(m => import(m))); // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore TODO options.cwd is possibly undefined, but it seems like no test covers this path const loader = await useCustomLoader(pointerOptions.loader, options.cwd); const result = await loader(pointer, { ...options, ...pointerOptions }, pointerOptionMap); - if (env['DEBUG'] != null) { - console.timeEnd(`@graphql-tools/load: collectCustomLoader ${pointer}`); - } + debugTimerEnd(`@graphql-tools/load: collectCustomLoader ${pointer}`); if (!result) { return; } @@ -253,9 +223,7 @@ function collectCustomLoaderSync( ) { if (pointerOptions.loader) { return queue(() => { - if (env['DEBUG'] != null) { - console.time(`@graphql-tools/load: collectCustomLoaderSync ${pointer}`); - } + debugTimerStart(`@graphql-tools/load: collectCustomLoaderSync ${pointer}`); const cwdRequire = createRequire(options.cwd || cwd()); for (const m of asArray(pointerOptions.require)) { cwdRequire(m); @@ -265,9 +233,7 @@ function collectCustomLoaderSync( const loader = useCustomLoaderSync(pointerOptions.loader, options.cwd); const result = loader(pointer, { ...options, ...pointerOptions }, pointerOptionMap); - if (env['DEBUG'] != null) { - console.timeEnd(`@graphql-tools/load: collectCustomLoaderSync ${pointer}`); - } + debugTimerEnd(`@graphql-tools/load: collectCustomLoaderSync ${pointer}`); if (result) { addResultOfCustomLoader({ pointer, result, addSource }); } @@ -285,9 +251,7 @@ function collectFallback({ addSource, }: CollectOptions) { return queue(async () => { - if (env['DEBUG'] != null) { - console.time(`@graphql-tools/load: collectFallback ${pointer}`); - } + debugTimerStart(`@graphql-tools/load: collectFallback ${pointer}`); const sources = await loadFile(pointer, { ...options, ...pointerOptions, @@ -298,9 +262,7 @@ function collectFallback({ addSource({ source, pointer }); } } - if (env['DEBUG'] != null) { - console.timeEnd(`@graphql-tools/load: collectFallback ${pointer}`); - } + debugTimerEnd(`@graphql-tools/load: collectFallback ${pointer}`); }); } @@ -312,9 +274,7 @@ function collectFallbackSync({ addSource, }: CollectOptions) { return queue(() => { - if (env['DEBUG'] != null) { - console.time(`@graphql-tools/load: collectFallbackSync ${pointer}`); - } + debugTimerStart(`@graphql-tools/load: collectFallbackSync ${pointer}`); const sources = loadFileSync(pointer, { ...options, ...pointerOptions, @@ -325,8 +285,6 @@ function collectFallbackSync({ addSource({ source, pointer }); } } - if (env['DEBUG'] != null) { - console.timeEnd(`@graphql-tools/load: collectFallbackSync ${pointer}`); - } + debugTimerEnd(`@graphql-tools/load: collectFallbackSync ${pointer}`); }); } diff --git a/packages/load/src/load-typedefs/load-file.ts b/packages/load/src/load-typedefs/load-file.ts index 532cfa7957a..715462937d2 100644 --- a/packages/load/src/load-typedefs/load-file.ts +++ b/packages/load/src/load-typedefs/load-file.ts @@ -1,11 +1,9 @@ import { env } from 'process'; -import { Source } from '@graphql-tools/utils'; +import { debugTimerEnd, debugTimerStart, Source } from '@graphql-tools/utils'; import { LoadTypedefsOptions } from '../load-typedefs.js'; export async function loadFile(pointer: string, options: LoadTypedefsOptions): Promise { - if (env['DEBUG'] != null) { - console.time(`@graphql-tools/load: loadFile ${pointer}`); - } + debugTimerStart(`@graphql-tools/load: loadFile ${pointer}`); let results = options.cache?.[pointer]; if (!results) { @@ -47,17 +45,13 @@ export async function loadFile(pointer: string, options: LoadTypedefsOptions): P } } - if (env['DEBUG'] != null) { - console.timeEnd(`@graphql-tools/load: loadFile ${pointer}`); - } + debugTimerEnd(`@graphql-tools/load: loadFile ${pointer}`); return results; } export function loadFileSync(pointer: string, options: LoadTypedefsOptions): Source[] { - if (env['DEBUG'] != null) { - console.time(`@graphql-tools/load: loadFileSync ${pointer}`); - } + debugTimerStart(`@graphql-tools/load: loadFileSync ${pointer}`); let results = options.cache?.[pointer]; if (!results) { @@ -99,9 +93,7 @@ export function loadFileSync(pointer: string, options: LoadTypedefsOptions): Sou } } - if (env['DEBUG'] != null) { - console.timeEnd(`@graphql-tools/load: loadFileSync ${pointer}`); - } + debugTimerEnd(`@graphql-tools/load: loadFileSync ${pointer}`); return results; } diff --git a/packages/load/src/load-typedefs/parse.ts b/packages/load/src/load-typedefs/parse.ts index b59c02a3b51..9a12a104521 100644 --- a/packages/load/src/load-typedefs/parse.ts +++ b/packages/load/src/load-typedefs/parse.ts @@ -1,5 +1,6 @@ -import { env } from 'process'; import { + debugTimerEnd, + debugTimerStart, parseGraphQLSDL, printSchemaWithDirectives, printWithComments, @@ -27,10 +28,8 @@ export function parseSource({ pointerOptionMap, addValidSource, }: ParseOptions) { - if (env['DEBUG'] != null) { - console.time(`@graphql-tools/load: parseSource ${partialSource.location}`); - } if (partialSource) { + debugTimerStart(`@graphql-tools/load: parseSource ${partialSource.location}`); const input = prepareInput({ source: partialSource, options, @@ -45,9 +44,7 @@ export function parseSource({ useComments(input); collectValidSources(input, addValidSource); } - } - if (env['DEBUG'] != null) { - console.timeEnd(`@graphql-tools/load: parseSource ${partialSource.location}`); + debugTimerEnd(`@graphql-tools/load: parseSource ${partialSource.location}`); } } @@ -77,63 +74,46 @@ function prepareInput({ } function parseSchema(input: Input) { - if (env['DEBUG'] != null) { - console.time(`@graphql-tools/load: parseSchema ${input.source.location}`); - } if (input.source.schema) { + debugTimerStart(`@graphql-tools/load: parseSchema ${input.source.location}`); input.source.rawSDL = printSchemaWithDirectives(input.source.schema, input.options); - } - if (env['DEBUG'] != null) { - console.timeEnd(`@graphql-tools/load: parseSchema ${input.source.location}`); + debugTimerEnd(`@graphql-tools/load: parseSchema ${input.source.location}`); } } function parseRawSDL(input: Input) { - if (env['DEBUG'] != null) { - console.time(`@graphql-tools/load: parseRawSDL ${input.source.location}`); - } if (input.source.rawSDL) { + debugTimerStart(`@graphql-tools/load: parseRawSDL ${input.source.location}`); input.source.document = parseGraphQLSDL( input.source.location, input.source.rawSDL, input.options, ).document; - } - if (env['DEBUG'] != null) { - console.timeEnd(`@graphql-tools/load: parseRawSDL ${input.source.location}`); + debugTimerEnd(`@graphql-tools/load: parseRawSDL ${input.source.location}`); } } function useKindsFilter(input: Input) { - if (env['DEBUG'] != null) { - console.time(`@graphql-tools/load: useKindsFilter ${input.source.location}`); - } if (input.options.filterKinds) { + debugTimerStart(`@graphql-tools/load: useKindsFilter ${input.source.location}`); input.source.document = filterKind(input.source.document, input.options.filterKinds); + debugTimerEnd(`@graphql-tools/load: useKindsFilter ${input.source.location}`); } } function useComments(input: Input) { - if (env['DEBUG'] != null) { - console.time(`@graphql-tools/load: useComments ${input.source.location}`); - } if (!input.source.rawSDL && input.source.document) { + debugTimerStart(`@graphql-tools/load: useComments ${input.source.location}`); input.source.rawSDL = printWithComments(input.source.document); resetComments(); - } - if (env['DEBUG'] != null) { - console.timeEnd(`@graphql-tools/load: useComments ${input.source.location}`); + debugTimerEnd(`@graphql-tools/load: useComments ${input.source.location}`); } } function collectValidSources(input: Input, addValidSource: AddValidSource) { - if (env['DEBUG'] != null) { - console.time(`@graphql-tools/load: collectValidSources ${input.source.location}`); - } if (input.source.document?.definitions && input.source.document.definitions.length > 0) { + debugTimerStart(`@graphql-tools/load: collectValidSources ${input.source.location}`); addValidSource(input.source); - } - if (env['DEBUG'] != null) { - console.timeEnd(`@graphql-tools/load: collectValidSources ${input.source.location}`); + debugTimerEnd(`@graphql-tools/load: collectValidSources ${input.source.location}`); } } diff --git a/packages/load/src/utils/pointers.ts b/packages/load/src/utils/pointers.ts index cd944159957..2f546508f62 100644 --- a/packages/load/src/utils/pointers.ts +++ b/packages/load/src/utils/pointers.ts @@ -1,13 +1,10 @@ -import { env } from 'process'; -import { asArray } from '@graphql-tools/utils'; +import { asArray, debugTimerEnd, debugTimerStart } from '@graphql-tools/utils'; import { UnnormalizedTypeDefPointer } from './../load-typedefs.js'; export function normalizePointers( unnormalizedPointerOrPointers: UnnormalizedTypeDefPointer | UnnormalizedTypeDefPointer[], ) { - if (env['DEBUG'] != null) { - console.time(`@graphql-tools/load: normalizePointers`); - } + debugTimerStart('@graphql-tools/load: normalizePointers'); const ignore: string[] = []; const pointerOptionMap: Record> = {}; @@ -20,9 +17,7 @@ export function normalizePointers( }; for (const rawPointer of asArray(unnormalizedPointerOrPointers)) { - if (env['DEBUG'] != null) { - console.time(`@graphql-tools/load: normalizePointers ${rawPointer}`); - } + debugTimerStart(`@graphql-tools/load: normalizePointers ${rawPointer}`); if (typeof rawPointer === 'string') { handlePointer(rawPointer); } else if (typeof rawPointer === 'object') { @@ -32,12 +27,8 @@ export function normalizePointers( } else { throw new Error(`Invalid pointer '${rawPointer}'.`); } - if (env['DEBUG'] != null) { - console.timeEnd(`@graphql-tools/load: normalizePointers ${rawPointer}`); - } - } - if (env['DEBUG'] != null) { - console.timeEnd(`@graphql-tools/load: normalizePointers`); + debugTimerEnd(`@graphql-tools/load: normalizePointers ${rawPointer}`); } + debugTimerEnd('@graphql-tools/load: normalizePointers'); return { ignore, pointerOptionMap }; } diff --git a/packages/utils/src/debugTimer.ts b/packages/utils/src/debugTimer.ts new file mode 100644 index 00000000000..47f20ed8f36 --- /dev/null +++ b/packages/utils/src/debugTimer.ts @@ -0,0 +1,15 @@ +const debugNamesOngoing = new Set(); + +export function debugTimerStart(name: string) { + const debugEnvVar = globalThis?.process.env['DEBUG'] || (globalThis as any).DEBUG; + if (debugEnvVar === '1' || debugEnvVar?.includes(name)) { + debugNamesOngoing.add(name); + console.time(name); + } +} + +export function debugTimerEnd(name: string) { + if (debugNamesOngoing.has(name)) { + console.timeEnd(name); + } +} diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index e2da68e528d..c2bd2e5ab05 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -53,3 +53,4 @@ export * from './Path.js'; export * from './jsutils.js'; export * from './directives.js'; export * from './mergeIncrementalResult.js'; +export * from './debugTimer.js';