Skip to content

Commit c92f3c4

Browse files
Merge pull request #2092 from nestjs/fix/support-nested-arrays
fix(graphql): support nested arrays when generating type defs
2 parents 5ab6096 + 23283c6 commit c92f3c4

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

packages/apollo/tests/generated-definitions/array-property.fixture.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ export interface Foo {
1212
b?: Nullable<string[]>;
1313
c: Nullable<string>[];
1414
d?: Nullable<Nullable<string>[]>;
15+
e: string[][];
16+
f: string[][][];
17+
g?: Nullable<Nullable<Nullable<Nullable<string>[]>[]>[]>;
1518
}
1619

1720
type Nullable<T> = T | null;

packages/apollo/tests/generated-definitions/array-property.graphql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@ type Foo {
33
b: [String!]
44
c: [String]!
55
d: [String]
6+
e: [[String!]!]!
7+
f: [[[String!]!]!]!
8+
g: [[[String]]]
69
}

packages/graphql/lib/graphql-ast.explorer.ts

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -326,42 +326,37 @@ export class GraphQLAstExplorer {
326326
name: string;
327327
required: boolean;
328328
} {
329-
const { required, type } = this.getNestedType(typeNode);
330-
331-
const isArray = type.kind === 'ListType';
332-
if (isArray) {
333-
const { type: arrayType, required: arrayTypeRequired } =
334-
this.getNestedType(get(type, 'type'));
335-
336-
const typeName = this.addSymbolIfRoot(get(arrayType, 'name.value'));
337-
const name = arrayTypeRequired
329+
const stringifyType = (typeNode: TypeNode) => {
330+
const { type, required } = this.unwrapTypeIfNonNull(typeNode);
331+
const isArray = type.kind === 'ListType';
332+
333+
if (isArray) {
334+
const arrayType = get(type, 'type');
335+
return required
336+
? `${stringifyType(arrayType)}[]`
337+
: `Nullable<${stringifyType(arrayType)}[]>`;
338+
}
339+
const typeName = this.addSymbolIfRoot(get(type, 'name.value'));
340+
return required
338341
? this.getType(typeName, options)
339342
: `Nullable<${this.getType(typeName, options)}>`;
343+
};
340344

341-
return {
342-
name: required ? name + '[]' : `Nullable<${name}[]>`,
343-
required,
344-
};
345-
}
346-
347-
const typeName = this.addSymbolIfRoot(get(type, 'name.value'));
348-
345+
const { required } = this.unwrapTypeIfNonNull(typeNode);
349346
return {
350-
name: required
351-
? this.getType(typeName, options)
352-
: `Nullable<${this.getType(typeName, options)}>`,
347+
name: stringifyType(typeNode),
353348
required,
354349
};
355350
}
356351

357-
getNestedType(type: TypeNode): {
352+
unwrapTypeIfNonNull(type: TypeNode): {
358353
type: TypeNode;
359354
required: boolean;
360355
} {
361356
const isNonNullType = type.kind === 'NonNullType';
362357
if (isNonNullType) {
363358
return {
364-
type: this.getNestedType(get(type, 'type')).type,
359+
type: this.unwrapTypeIfNonNull(get(type, 'type')).type,
365360
required: isNonNullType,
366361
};
367362
}

0 commit comments

Comments
 (0)