Skip to content

Commit

Permalink
fix(load): respect filters in DEBUG env var (#5715)
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Nov 23, 2023
1 parent 3863a82 commit d3fe8d8
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 148 deletions.
6 changes: 6 additions & 0 deletions .changeset/purple-owls-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphql-tools/utils': patch
'@graphql-tools/load': patch
---

Print debug timer logs by respecting the filters in DEBUG env var
36 changes: 16 additions & 20 deletions packages/load/src/load-typedefs.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -30,9 +37,7 @@ export async function loadTypedefs<AdditionalConfig = Record<string, unknown>>(
pointerOrPointers: UnnormalizedTypeDefPointer | UnnormalizedTypeDefPointer[],
options: LoadTypedefsOptions<Partial<AdditionalConfig>>,
): Promise<Source[]> {
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 || []);
Expand Down Expand Up @@ -67,9 +72,7 @@ export async function loadTypedefs<AdditionalConfig = Record<string, unknown>>(

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

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

return result;
}
Expand All @@ -85,9 +88,7 @@ export function loadTypedefsSync<AdditionalConfig = Record<string, unknown>>(
pointerOrPointers: UnnormalizedTypeDefPointer | UnnormalizedTypeDefPointer[],
options: LoadTypedefsOptions<Partial<AdditionalConfig>>,
): 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);
Expand All @@ -114,9 +115,7 @@ export function loadTypedefsSync<AdditionalConfig = Record<string, unknown>>(

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

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

return result;
}
Expand All @@ -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) {
Expand All @@ -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;
}
92 changes: 25 additions & 67 deletions packages/load/src/load-typedefs/collect-sources.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -28,9 +30,7 @@ export async function collectSources<TOptions>({
};
options: LoadTypedefsOptions<Partial<TOptions>>;
}): Promise<Source[]> {
if (env['DEBUG'] != null) {
console.time('@graphql-tools/load: collectSources');
}
debugTimerStart('@graphql-tools/load: collectSources');
const sources: Source[] = [];
const queue = useQueue<void>({ concurrency: CONCURRENCY_LIMIT });

Expand All @@ -42,9 +42,7 @@ export async function collectSources<TOptions>({
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,
Expand All @@ -53,18 +51,12 @@ export async function collectSources<TOptions>({
addSource,
queue: queue.add as AddToQueue<void>,
});
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;
}

Expand All @@ -85,16 +77,12 @@ export function collectSourcesSync<TOptions>({
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,
Expand All @@ -103,19 +91,13 @@ export function collectSourcesSync<TOptions>({
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;
}

Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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<T>(
{ pointer, pointerOptions, options, addSource, queue }: CollectOptions<T>,
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, {
Expand All @@ -211,9 +187,7 @@ function collectDocumentString<T>(
});
});
}
if (env['DEBUG'] != null) {
console.timeEnd(`@graphql-tools/load: collectDocumentString ${pointer}`);
}
debugTimerEnd(`@graphql-tools/load: collectDocumentString ${pointer}`);

next();
}
Expand All @@ -224,18 +198,14 @@ function collectCustomLoader<T>(
) {
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;
}
Expand All @@ -253,9 +223,7 @@ function collectCustomLoaderSync<T>(
) {
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);
Expand All @@ -265,9 +233,7 @@ function collectCustomLoaderSync<T>(
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 });
}
Expand All @@ -285,9 +251,7 @@ function collectFallback<T>({
addSource,
}: CollectOptions<T>) {
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,
Expand All @@ -298,9 +262,7 @@ function collectFallback<T>({
addSource({ source, pointer });
}
}
if (env['DEBUG'] != null) {
console.timeEnd(`@graphql-tools/load: collectFallback ${pointer}`);
}
debugTimerEnd(`@graphql-tools/load: collectFallback ${pointer}`);
});
}

Expand All @@ -312,9 +274,7 @@ function collectFallbackSync<T>({
addSource,
}: CollectOptions<T>) {
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,
Expand All @@ -325,8 +285,6 @@ function collectFallbackSync<T>({
addSource({ source, pointer });
}
}
if (env['DEBUG'] != null) {
console.timeEnd(`@graphql-tools/load: collectFallbackSync ${pointer}`);
}
debugTimerEnd(`@graphql-tools/load: collectFallbackSync ${pointer}`);
});
}
18 changes: 5 additions & 13 deletions packages/load/src/load-typedefs/load-file.ts
Original file line number Diff line number Diff line change
@@ -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<Source[]> {
if (env['DEBUG'] != null) {
console.time(`@graphql-tools/load: loadFile ${pointer}`);
}
debugTimerStart(`@graphql-tools/load: loadFile ${pointer}`);
let results = options.cache?.[pointer];

if (!results) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}

0 comments on commit d3fe8d8

Please sign in to comment.