From 35621e4ce7dc23a998da86af921345a8cf81d842 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils-B=C3=B6rge=20Margotti?= Date: Tue, 31 Dec 2024 15:09:02 +0100 Subject: [PATCH 1/5] Update getIntrospectionQuery.ts to allow configuration of the type query depth This allows for a better configuration in case the server restricts the maximum query depth. --- src/utilities/getIntrospectionQuery.ts | 58 ++++++++++---------------- 1 file changed, 22 insertions(+), 36 deletions(-) diff --git a/src/utilities/getIntrospectionQuery.ts b/src/utilities/getIntrospectionQuery.ts index d4bca051c1..5193a9593a 100644 --- a/src/utilities/getIntrospectionQuery.ts +++ b/src/utilities/getIntrospectionQuery.ts @@ -44,6 +44,15 @@ export interface IntrospectionOptions { * Default: false */ oneOf?: boolean; + + /** + * How deep to recurse into nested types. Larger values will result in more + * accurate results, but have a higher load. Some servers might restrict the + * maximum query depth. If thats the case, try decreasing this value. + * + * Default: 9 + */ + typeDepth?: number; } /** @@ -59,6 +68,7 @@ export function getIntrospectionQuery(options?: IntrospectionOptions): string { inputValueDeprecation: false, experimentalDirectiveDeprecation: false, oneOf: false, + typeDepth: 9, ...options, }; @@ -80,6 +90,17 @@ export function getIntrospectionQuery(options?: IntrospectionOptions): string { return optionsWithDefault.experimentalDirectiveDeprecation ? str : ''; } const oneOf = optionsWithDefault.oneOf ? 'isOneOf' : ''; + function ofType(level = 9): string { + if (level <= 0) { + return ''; + } + return ` + ofType { + name + kind + ${ofType(level - 1)} + }`; + } return ` query IntrospectionQuery { @@ -154,42 +175,7 @@ export function getIntrospectionQuery(options?: IntrospectionOptions): string { fragment TypeRef on __Type { kind name - ofType { - kind - name - ofType { - kind - name - ofType { - kind - name - ofType { - kind - name - ofType { - kind - name - ofType { - kind - name - ofType { - kind - name - ofType { - kind - name - ofType { - kind - name - } - } - } - } - } - } - } - } - } + ${ofType(optionsWithDefault.typeDepth)} } `; } From 1e411a30812d0d774d407a10797f0c491ef6a2a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils-B=C3=B6rge=20Margotti?= Date: Fri, 3 Jan 2025 13:32:59 +0100 Subject: [PATCH 2/5] Update template function to handle formatting Co-authored-by: Benjie --- src/utilities/getIntrospectionQuery.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/utilities/getIntrospectionQuery.ts b/src/utilities/getIntrospectionQuery.ts index 5193a9593a..b2b2fd3eca 100644 --- a/src/utilities/getIntrospectionQuery.ts +++ b/src/utilities/getIntrospectionQuery.ts @@ -90,16 +90,18 @@ export function getIntrospectionQuery(options?: IntrospectionOptions): string { return optionsWithDefault.experimentalDirectiveDeprecation ? str : ''; } const oneOf = optionsWithDefault.oneOf ? 'isOneOf' : ''; - function ofType(level = 9): string { + function ofType(level: number, indent: string): string { if (level <= 0) { return ''; } + if (level > 100) { + throw new Error("Please set typeDepth to a reasonable value; the default is 9."); + } return ` - ofType { - name - kind - ${ofType(level - 1)} - }`; +${indent}ofType { +${indent} name +${indent} kind${ofType(level - 1, indent + " ")} +${indent}}`; } return ` @@ -174,8 +176,7 @@ export function getIntrospectionQuery(options?: IntrospectionOptions): string { fragment TypeRef on __Type { kind - name - ${ofType(optionsWithDefault.typeDepth)} + name${ofType(optionsWithDefault.typeDepth ?? 9, " ")} } `; } From f508c350242d5503321beaf0f429b9f5c892a48b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils-B=C3=B6rge=20Margotti?= Date: Sun, 12 Jan 2025 21:48:04 +0100 Subject: [PATCH 3/5] Apply suggestions from code review Co-authored-by: Jovi De Croock --- src/utilities/getIntrospectionQuery.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/utilities/getIntrospectionQuery.ts b/src/utilities/getIntrospectionQuery.ts index b2b2fd3eca..d8acf6e87b 100644 --- a/src/utilities/getIntrospectionQuery.ts +++ b/src/utilities/getIntrospectionQuery.ts @@ -46,9 +46,10 @@ export interface IntrospectionOptions { oneOf?: boolean; /** - * How deep to recurse into nested types. Larger values will result in more - * accurate results, but have a higher load. Some servers might restrict the - * maximum query depth. If thats the case, try decreasing this value. + * How deep to recurse into nested types, larger values will result in more + * accurate results, but have a higher load on the server. + * Some servers might restrict the maximum query depth or complexity. + * If that's the case, try decreasing this value. * * Default: 9 */ @@ -95,12 +96,12 @@ export function getIntrospectionQuery(options?: IntrospectionOptions): string { return ''; } if (level > 100) { - throw new Error("Please set typeDepth to a reasonable value; the default is 9."); + throw new Error('Please set typeDepth to a reasonable value; the default is 9.'); } return ` ${indent}ofType { ${indent} name -${indent} kind${ofType(level - 1, indent + " ")} +${indent} kind${ofType(level - 1, indent + ' ')} ${indent}}`; } @@ -176,7 +177,7 @@ ${indent}}`; fragment TypeRef on __Type { kind - name${ofType(optionsWithDefault.typeDepth ?? 9, " ")} + name${ofType(optionsWithDefault.typeDepth ?? 9, ' ')} } `; } From ae75d6715198a3652397e5cc8646146f2a629c7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils-B=C3=B6rge=20Margotti?= Date: Sun, 12 Jan 2025 22:05:40 +0100 Subject: [PATCH 4/5] Add tests --- src/utilities/__tests__/getIntrospectionQuery-test.ts | 6 ++++++ src/utilities/getIntrospectionQuery.ts | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/utilities/__tests__/getIntrospectionQuery-test.ts b/src/utilities/__tests__/getIntrospectionQuery-test.ts index 656412022c..fe7742e9b4 100644 --- a/src/utilities/__tests__/getIntrospectionQuery-test.ts +++ b/src/utilities/__tests__/getIntrospectionQuery-test.ts @@ -176,4 +176,10 @@ describe('getIntrospectionQuery', () => { .toNotContain('directives(includeDeprecated: true) {') .toMatch('deprecationReason', 2); }); + + it('throw error if typeDepth is too high', () => { + expect(() => getIntrospectionQuery({ typeDepth: 101 })).to.throw( + 'Please set typeDepth to a reasonable value between 0 and 100; the default is 9.', + ); + }); }); diff --git a/src/utilities/getIntrospectionQuery.ts b/src/utilities/getIntrospectionQuery.ts index d8acf6e87b..535e469fe2 100644 --- a/src/utilities/getIntrospectionQuery.ts +++ b/src/utilities/getIntrospectionQuery.ts @@ -96,7 +96,9 @@ export function getIntrospectionQuery(options?: IntrospectionOptions): string { return ''; } if (level > 100) { - throw new Error('Please set typeDepth to a reasonable value; the default is 9.'); + throw new Error( + 'Please set typeDepth to a reasonable value between 0 and 100; the default is 9.', + ); } return ` ${indent}ofType { From 2dca061c868cfb6b3cdb54fb1472eac2a7bcd59a Mon Sep 17 00:00:00 2001 From: Yaacov Rydzinski Date: Sat, 25 Apr 2026 21:15:01 +0300 Subject: [PATCH 5/5] TS ensures this is not null/undefined --- src/utilities/getIntrospectionQuery.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utilities/getIntrospectionQuery.ts b/src/utilities/getIntrospectionQuery.ts index 535e469fe2..761b50c7ef 100644 --- a/src/utilities/getIntrospectionQuery.ts +++ b/src/utilities/getIntrospectionQuery.ts @@ -179,7 +179,7 @@ ${indent}}`; fragment TypeRef on __Type { kind - name${ofType(optionsWithDefault.typeDepth ?? 9, ' ')} + name${ofType(optionsWithDefault.typeDepth, ' ')} } `; }