Skip to content

Commit 8eb6383

Browse files
Nols1000benjieJoviDeCroockyaacovCR
authored
Allow configuration of the ofType introspection depth (#4317)
This allows for a better configuration in case the server restricts the maximum query depth. --- Added by @benjie: - Fixes #4321 --------- Co-authored-by: Benjie <benjie@jemjie.com> Co-authored-by: Jovi De Croock <decroockjovi@gmail.com> Co-authored-by: Yaacov Rydzinski <yaacovCR@gmail.com>
1 parent ad9c519 commit 8eb6383

2 files changed

Lines changed: 33 additions & 37 deletions

File tree

src/utilities/__tests__/getIntrospectionQuery-test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,10 @@ describe('getIntrospectionQuery', () => {
176176
.toNotContain('directives(includeDeprecated: true) {')
177177
.toMatch('deprecationReason', 2);
178178
});
179+
180+
it('throw error if typeDepth is too high', () => {
181+
expect(() => getIntrospectionQuery({ typeDepth: 101 })).to.throw(
182+
'Please set typeDepth to a reasonable value between 0 and 100; the default is 9.',
183+
);
184+
});
179185
});

src/utilities/getIntrospectionQuery.ts

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ export interface IntrospectionOptions {
4444
* Default: false
4545
*/
4646
oneOf?: boolean;
47+
48+
/**
49+
* How deep to recurse into nested types, larger values will result in more
50+
* accurate results, but have a higher load on the server.
51+
* Some servers might restrict the maximum query depth or complexity.
52+
* If that's the case, try decreasing this value.
53+
*
54+
* Default: 9
55+
*/
56+
typeDepth?: number;
4757
}
4858

4959
/**
@@ -59,6 +69,7 @@ export function getIntrospectionQuery(options?: IntrospectionOptions): string {
5969
inputValueDeprecation: false,
6070
experimentalDirectiveDeprecation: false,
6171
oneOf: false,
72+
typeDepth: 9,
6273
...options,
6374
};
6475

@@ -80,6 +91,21 @@ export function getIntrospectionQuery(options?: IntrospectionOptions): string {
8091
return optionsWithDefault.experimentalDirectiveDeprecation ? str : '';
8192
}
8293
const oneOf = optionsWithDefault.oneOf ? 'isOneOf' : '';
94+
function ofType(level: number, indent: string): string {
95+
if (level <= 0) {
96+
return '';
97+
}
98+
if (level > 100) {
99+
throw new Error(
100+
'Please set typeDepth to a reasonable value between 0 and 100; the default is 9.',
101+
);
102+
}
103+
return `
104+
${indent}ofType {
105+
${indent} name
106+
${indent} kind${ofType(level - 1, indent + ' ')}
107+
${indent}}`;
108+
}
83109

84110
return `
85111
query IntrospectionQuery {
@@ -153,43 +179,7 @@ export function getIntrospectionQuery(options?: IntrospectionOptions): string {
153179
154180
fragment TypeRef on __Type {
155181
kind
156-
name
157-
ofType {
158-
kind
159-
name
160-
ofType {
161-
kind
162-
name
163-
ofType {
164-
kind
165-
name
166-
ofType {
167-
kind
168-
name
169-
ofType {
170-
kind
171-
name
172-
ofType {
173-
kind
174-
name
175-
ofType {
176-
kind
177-
name
178-
ofType {
179-
kind
180-
name
181-
ofType {
182-
kind
183-
name
184-
}
185-
}
186-
}
187-
}
188-
}
189-
}
190-
}
191-
}
192-
}
182+
name${ofType(optionsWithDefault.typeDepth, ' ')}
193183
}
194184
`;
195185
}

0 commit comments

Comments
 (0)