Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

graphql-tools/load: use debug to log timers and errors #4886

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/silver-dingos-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-tools/load': minor
---

Enable debug logs via DEBUG=graphql-tools/load instead of DEBUG=true or DEBUG=1
3 changes: 2 additions & 1 deletion packages/load/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@
"graphql-type-json": "0.3.2"
},
"dependencies": {
"@graphql-tools/utils": "9.1.1",
"@graphql-tools/schema": "9.0.10",
"@graphql-tools/utils": "9.1.1",
"debug": "4.3.4",
"p-limit": "3.1.0",
"tslib": "^2.4.0"
},
Expand Down
8 changes: 3 additions & 5 deletions packages/load/src/filter-document-kind.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DocumentNode, DefinitionNode, Kind } from 'graphql';
import { env } from 'process';
import { log } from './utils/debug.js';

/**
* @internal
Expand All @@ -20,10 +20,8 @@ export const filterKind = (
}

if (invalidDefinitions.length > 0) {
if (env['DEBUG']) {
for (const d of invalidDefinitions) {
console.log(`Filtered document of kind ${d.kind} due to filter policy (${filterKinds.join(', ')})`);
}
for (const d of invalidDefinitions) {
log(`Filtered document of kind ${d.kind} due to filter policy (${filterKinds.join(', ')})`);
}
}

Expand Down
26 changes: 7 additions & 19 deletions packages/load/src/load-typedefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { applyDefaultOptions } from './load-typedefs/options.js';
import { collectSources, collectSourcesSync } from './load-typedefs/collect-sources.js';
import { parseSource } from './load-typedefs/parse.js';
import { useLimit } from './utils/helpers.js';
import { env } from 'process';
import { time, timeEnd } from './utils/debug.js';

const CONCURRENCY_LIMIT = 100;

Expand All @@ -30,9 +30,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');
}
time('loadTypedefs');
const { ignore, pointerOptionMap } = normalizePointers(pointerOrPointers);

options.ignore = asArray(options.ignore || []);
Expand Down Expand Up @@ -67,9 +65,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');
}
timeEnd('loadTypedefs');

return result;
}
Expand All @@ -85,9 +81,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');
}
time('loadTypedefsSync');
const { ignore, pointerOptionMap } = normalizePointers(pointerOrPointers);

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

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

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

return result;
}
Expand All @@ -132,9 +124,7 @@ function prepareResult({
pointerOptionMap: any;
validSources: Source[];
}) {
if (env['DEBUG'] != null) {
console.time('@graphql-tools/load: prepareResult');
}
time('prepareResult');
const pointerList = Object.keys(pointerOptionMap);

if (pointerList.length > 0 && validSources.length === 0) {
Expand All @@ -150,8 +140,6 @@ 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');
}
timeEnd('prepareResult');
return sortedResult;
}
91 changes: 24 additions & 67 deletions packages/load/src/load-typedefs/collect-sources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { stringToHash, useStack, StackNext, StackFn } from '../utils/helpers.js'
import { useCustomLoader, useCustomLoaderSync } from '../utils/custom-loader.js';
import { useQueue, useSyncQueue } from '../utils/queue.js';
import { createRequire } from 'module';
import { cwd, env } from 'process';
import { cwd } from 'process';
import { time, timeEnd } from '../utils/debug.js';

type AddSource = (data: { pointer: string; source: Source; noCache?: boolean }) => void;
type AddToQueue<T> = (fn: () => Promise<T> | T) => void;
Expand All @@ -22,9 +23,7 @@ export async function collectSources<TOptions>({
};
options: LoadTypedefsOptions<Partial<TOptions>>;
}): Promise<Source[]> {
if (env['DEBUG'] != null) {
console.time('@graphql-tools/load: collectSources');
}
time('collectSources');
const sources: Source[] = [];
const queue = useQueue<void>({ concurrency: CONCURRENCY_LIMIT });

Expand All @@ -36,9 +35,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}`);
}
time(`collectSources ${pointer}`);
collect({
pointer,
pointerOptions,
Expand All @@ -47,18 +44,12 @@ export async function collectSources<TOptions>({
addSource,
queue: queue.add as AddToQueue<void>,
});
if (env['DEBUG'] != null) {
console.timeEnd(`@graphql-tools/load: collectSources ${pointer}`);
}
timeEnd(`collectSources ${pointer}`);
}

if (env['DEBUG'] != null) {
console.time('@graphql-tools/load: collectSources queue');
}
time('collectSources queue');
await queue.runAll();
if (env['DEBUG'] != null) {
console.timeEnd('@graphql-tools/load: collectSources queue');
}
timeEnd('collectSources queue');
return sources;
}

Expand All @@ -79,16 +70,12 @@ export function collectSourcesSync<TOptions>({
stack: [collectDocumentString, collectCustomLoaderSync, collectFallbackSync],
});

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

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

if (env['DEBUG'] != null) {
console.time(`@graphql-tools/load: collectSourcesSync ${pointer}`);
}
time(`collectSourcesSync ${pointer}`);
collect({
pointer,
pointerOptions,
Expand All @@ -97,19 +84,13 @@ export function collectSourcesSync<TOptions>({
addSource,
queue: queue.add,
});
if (env['DEBUG'] != null) {
console.timeEnd(`@graphql-tools/load: collectSourcesSync ${pointer}`);
}
timeEnd(`collectSourcesSync ${pointer}`);
}

if (env['DEBUG'] != null) {
console.time('@graphql-tools/load: collectSourcesSync queue');
}
time('collectSourcesSync queue');
queue.runAll();

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

Expand Down Expand Up @@ -144,9 +125,7 @@ function addResultOfCustomLoader({
result: any;
addSource: AddSource;
}) {
if (env['DEBUG'] != null) {
console.time(`@graphql-tools/load: addResultOfCustomLoader ${pointer}`);
}
time(`addResultOfCustomLoader ${pointer}`);
if (isSchema(result)) {
addSource({
source: {
Expand Down Expand Up @@ -174,18 +153,14 @@ function addResultOfCustomLoader({
pointer,
});
}
if (env['DEBUG'] != null) {
console.timeEnd(`@graphql-tools/load: addResultOfCustomLoader ${pointer}`);
}
timeEnd(`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}`);
}
time(`collectDocumentString ${pointer}`);
if (isDocumentString(pointer)) {
return queue(() => {
const source = parseGraphQLSDL(`${stringToHash(pointer)}.graphql`, pointer, {
Expand All @@ -199,9 +174,7 @@ function collectDocumentString<T>(
});
});
}
if (env['DEBUG'] != null) {
console.timeEnd(`@graphql-tools/load: collectDocumentString ${pointer}`);
}
timeEnd(`collectDocumentString ${pointer}`);

next();
}
Expand All @@ -212,18 +185,14 @@ function collectCustomLoader<T>(
) {
if (pointerOptions.loader) {
return queue(async () => {
if (env['DEBUG'] != null) {
console.time(`@graphql-tools/load: collectCustomLoader ${pointer}`);
}
time(`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}`);
}
timeEnd(`collectCustomLoader ${pointer}`);
if (!result) {
return;
}
Expand All @@ -241,9 +210,7 @@ function collectCustomLoaderSync<T>(
) {
if (pointerOptions.loader) {
return queue(() => {
if (env['DEBUG'] != null) {
console.time(`@graphql-tools/load: collectCustomLoaderSync ${pointer}`);
}
time(`collectCustomLoaderSync ${pointer}`);
const cwdRequire = createRequire(options.cwd || cwd());
for (const m of asArray(pointerOptions.require)) {
cwdRequire(m);
Expand All @@ -253,9 +220,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}`);
}
timeEnd(`collectCustomLoaderSync ${pointer}`);
if (result) {
addResultOfCustomLoader({ pointer, result, addSource });
}
Expand All @@ -267,9 +232,7 @@ function collectCustomLoaderSync<T>(

function collectFallback<T>({ queue, pointer, options, pointerOptions, addSource }: CollectOptions<T>) {
return queue(async () => {
if (env['DEBUG'] != null) {
console.time(`@graphql-tools/load: collectFallback ${pointer}`);
}
time(`collectFallback ${pointer}`);
const sources = await loadFile(pointer, {
...options,
...pointerOptions,
Expand All @@ -280,17 +243,13 @@ function collectFallback<T>({ queue, pointer, options, pointerOptions, addSource
addSource({ source, pointer });
}
}
if (env['DEBUG'] != null) {
console.timeEnd(`@graphql-tools/load: collectFallback ${pointer}`);
}
timeEnd(`collectFallback ${pointer}`);
});
}

function collectFallbackSync<T>({ queue, pointer, options, pointerOptions, addSource }: CollectOptions<T>) {
return queue(() => {
if (env['DEBUG'] != null) {
console.time(`@graphql-tools/load: collectFallbackSync ${pointer}`);
}
time(`collectFallbackSync ${pointer}`);
const sources = loadFileSync(pointer, {
...options,
...pointerOptions,
Expand All @@ -301,8 +260,6 @@ function collectFallbackSync<T>({ queue, pointer, options, pointerOptions, addSo
addSource({ source, pointer });
}
}
if (env['DEBUG'] != null) {
console.timeEnd(`@graphql-tools/load: collectFallbackSync ${pointer}`);
}
timeEnd(`collectFallbackSync ${pointer}`);
});
}
Loading