From 388d4056874fdfe2d82bfb24a6af634d5da1f5f7 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/4] 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 373b474ed5..dfa6cd2a98 100644 --- a/src/utilities/getIntrospectionQuery.ts +++ b/src/utilities/getIntrospectionQuery.ts @@ -38,6 +38,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; } /** @@ -52,6 +61,7 @@ export function getIntrospectionQuery(options?: IntrospectionOptions): string { schemaDescription: false, inputValueDeprecation: false, oneOf: false, + typeDepth: 9, ...options, }; @@ -70,6 +80,17 @@ export function getIntrospectionQuery(options?: IntrospectionOptions): string { return optionsWithDefault.inputValueDeprecation ? 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 { @@ -140,42 +161,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 366de1f623227a01554cab926c2aa5963ba4811b 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/4] 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 dfa6cd2a98..a4e1e3b9da 100644 --- a/src/utilities/getIntrospectionQuery.ts +++ b/src/utilities/getIntrospectionQuery.ts @@ -80,16 +80,18 @@ export function getIntrospectionQuery(options?: IntrospectionOptions): string { return optionsWithDefault.inputValueDeprecation ? 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 ` @@ -160,8 +162,7 @@ export function getIntrospectionQuery(options?: IntrospectionOptions): string { fragment TypeRef on __Type { kind - name - ${ofType(optionsWithDefault.typeDepth)} + name${ofType(optionsWithDefault.typeDepth ?? 9, " ")} } `; } From a6cb5cdb9e1e5c95e46daeb9fc6344688b60391e 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/4] 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 a4e1e3b9da..9effdce411 100644 --- a/src/utilities/getIntrospectionQuery.ts +++ b/src/utilities/getIntrospectionQuery.ts @@ -40,9 +40,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 */ @@ -85,12 +86,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}}`; } @@ -162,7 +163,7 @@ ${indent}}`; fragment TypeRef on __Type { kind - name${ofType(optionsWithDefault.typeDepth ?? 9, " ")} + name${ofType(optionsWithDefault.typeDepth ?? 9, ' ')} } `; } From 1881e0f154f819cfb90b2bad7daf9193aa4f2664 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/4] 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 86d1c549db..69f2290e6a 100644 --- a/src/utilities/__tests__/getIntrospectionQuery-test.ts +++ b/src/utilities/__tests__/getIntrospectionQuery-test.ts @@ -138,4 +138,10 @@ describe('getIntrospectionQuery', () => { 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 9effdce411..2f80845a5e 100644 --- a/src/utilities/getIntrospectionQuery.ts +++ b/src/utilities/getIntrospectionQuery.ts @@ -86,7 +86,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 {