Skip to content

Commit d0503e2

Browse files
committed
fix typeInfo.getParentInputType when out of type system
1 parent 87e20de commit d0503e2

2 files changed

Lines changed: 52 additions & 28 deletions

File tree

src/utilities/TypeInfo.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export interface FragmentSignature {
4949
*/
5050
export class TypeInfo {
5151
private _schema: GraphQLSchema;
52+
private _depthFromInputTypeSystem: number;
5253
private _typeStack: Array<Maybe<GraphQLOutputType>>;
5354
private _parentTypeStack: Array<Maybe<GraphQLCompositeType>>;
5455
private _inputTypeStack: Array<Maybe<GraphQLInputType>>;
@@ -76,6 +77,7 @@ export class TypeInfo {
7677
>,
7778
) {
7879
this._schema = schema;
80+
this._depthFromInputTypeSystem = -1;
7981
this._typeStack = [];
8082
this._parentTypeStack = [];
8183
this._inputTypeStack = [];
@@ -89,7 +91,7 @@ export class TypeInfo {
8991
this._fragmentArgument = null;
9092
if (initialType) {
9193
if (isInputType(initialType)) {
92-
this._inputTypeStack.push(initialType);
94+
this._pushInputType(initialType);
9395
}
9496
if (isCompositeType(initialType)) {
9597
this._parentTypeStack.push(initialType);
@@ -117,6 +119,9 @@ export class TypeInfo {
117119
}
118120

119121
getParentInputType(): Maybe<GraphQLInputType> {
122+
if (this._depthFromInputTypeSystem > 0) {
123+
return undefined;
124+
}
120125
return this._inputTypeStack.at(-2);
121126
}
122127

@@ -155,6 +160,9 @@ export class TypeInfo {
155160
}
156161

157162
enter(node: ASTNode): void {
163+
if (this._depthFromInputTypeSystem >= 0) {
164+
this._depthFromInputTypeSystem++;
165+
}
158166
const schema = this._schema;
159167
// Note: many of the types below are explicitly typed as "unknown" to drop
160168
// any assumptions of a valid schema to ensure runtime types are properly
@@ -213,9 +221,7 @@ export class TypeInfo {
213221
}
214222
case Kind.VARIABLE_DEFINITION: {
215223
const inputType: unknown = typeFromAST(schema, node.type);
216-
this._inputTypeStack.push(
217-
isInputType(inputType) ? inputType : undefined,
218-
);
224+
this._pushInputType(isInputType(inputType) ? inputType : undefined);
219225
break;
220226
}
221227
case Kind.ARGUMENT: {
@@ -234,7 +240,7 @@ export class TypeInfo {
234240
this._defaultValueStack.push(
235241
argDef?.default ?? argDef?.defaultValue ?? undefined,
236242
);
237-
this._inputTypeStack.push(isInputType(argType) ? argType : undefined);
243+
this._pushInputType(isInputType(argType) ? argType : undefined);
238244
break;
239245
}
240246
case Kind.FRAGMENT_ARGUMENT: {
@@ -247,7 +253,7 @@ export class TypeInfo {
247253
if (argDef) {
248254
argType = typeFromAST(this._schema, argDef.type);
249255
}
250-
this._inputTypeStack.push(isInputType(argType) ? argType : undefined);
256+
this._pushInputType(isInputType(argType) ? argType : undefined);
251257
break;
252258
}
253259
case Kind.LIST: {
@@ -257,7 +263,7 @@ export class TypeInfo {
257263
: undefined;
258264
// List positions never have a default value.
259265
this._defaultValueStack.push(undefined);
260-
this._inputTypeStack.push(isInputType(itemType) ? itemType : undefined);
266+
this._pushInputType(isInputType(itemType) ? itemType : undefined);
261267
break;
262268
}
263269
case Kind.OBJECT_FIELD: {
@@ -273,7 +279,7 @@ export class TypeInfo {
273279
this._defaultValueStack.push(
274280
inputField?.default ?? inputField?.defaultValue ?? undefined,
275281
);
276-
this._inputTypeStack.push(
282+
this._pushInputType(
277283
isInputType(inputFieldType) ? inputFieldType : undefined,
278284
);
279285
break;
@@ -341,6 +347,16 @@ export class TypeInfo {
341347
default:
342348
// Ignore other nodes
343349
}
350+
if (this._depthFromInputTypeSystem >= 0) {
351+
this._depthFromInputTypeSystem--;
352+
}
353+
}
354+
355+
private _pushInputType(type: Maybe<GraphQLInputType>): void {
356+
this._inputTypeStack.push(type);
357+
if (type === undefined && this._depthFromInputTypeSystem < 0) {
358+
this._depthFromInputTypeSystem = 0;
359+
}
344360
}
345361
}
346362

src/utilities/__tests__/TypeInfo-test.ts

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -535,20 +535,24 @@ describe('visitWithTypeInfo', () => {
535535
visitWithTypeInfo(typeInfo, {
536536
enter(node) {
537537
const type = typeInfo.getInputType();
538+
const parentType = typeInfo.getParentInputType();
538539
visited.push([
539540
'enter',
540541
node.kind,
541542
node.kind === 'Name' ? node.value : null,
542543
String(type),
544+
String(parentType),
543545
]);
544546
},
545547
leave(node) {
546548
const type = typeInfo.getInputType();
549+
const parentType = typeInfo.getParentInputType();
547550
visited.push([
548551
'leave',
549552
node.kind,
550553
node.kind === 'Name' ? node.value : null,
551554
String(type),
555+
String(parentType),
552556
]);
553557
},
554558
}),
@@ -557,20 +561,20 @@ describe('visitWithTypeInfo', () => {
557561
expect(visited).to.deep.equal([
558562
// Everything within ObjectValue should have type: undefined since the
559563
// contents of custom scalars are not part of the GraphQL type system.
560-
['enter', 'ObjectValue', null, 'GeoPoint'],
561-
['enter', 'ObjectField', null, 'undefined'],
562-
['enter', 'Name', 'x', 'undefined'],
563-
['leave', 'Name', 'x', 'undefined'],
564-
['enter', 'FloatValue', null, 'undefined'],
565-
['leave', 'FloatValue', null, 'undefined'],
566-
['leave', 'ObjectField', null, 'undefined'],
567-
['enter', 'ObjectField', null, 'undefined'],
568-
['enter', 'Name', 'y', 'undefined'],
569-
['leave', 'Name', 'y', 'undefined'],
570-
['enter', 'FloatValue', null, 'undefined'],
571-
['leave', 'FloatValue', null, 'undefined'],
572-
['leave', 'ObjectField', null, 'undefined'],
573-
['leave', 'ObjectValue', null, 'GeoPoint'],
564+
['enter', 'ObjectValue', null, 'GeoPoint', 'undefined'],
565+
['enter', 'ObjectField', null, 'undefined', 'GeoPoint'],
566+
['enter', 'Name', 'x', 'undefined', 'undefined'],
567+
['leave', 'Name', 'x', 'undefined', 'undefined'],
568+
['enter', 'FloatValue', null, 'undefined', 'undefined'],
569+
['leave', 'FloatValue', null, 'undefined', 'undefined'],
570+
['leave', 'ObjectField', null, 'undefined', 'GeoPoint'],
571+
['enter', 'ObjectField', null, 'undefined', 'GeoPoint'],
572+
['enter', 'Name', 'y', 'undefined', 'undefined'],
573+
['leave', 'Name', 'y', 'undefined', 'undefined'],
574+
['enter', 'FloatValue', null, 'undefined', 'undefined'],
575+
['leave', 'FloatValue', null, 'undefined', 'undefined'],
576+
['leave', 'ObjectField', null, 'undefined', 'GeoPoint'],
577+
['leave', 'ObjectValue', null, 'GeoPoint', 'undefined'],
574578
]);
575579
});
576580

@@ -590,20 +594,24 @@ describe('visitWithTypeInfo', () => {
590594
visitWithTypeInfo(typeInfo, {
591595
enter(node) {
592596
const type = typeInfo.getInputType();
597+
const parentType = typeInfo.getParentInputType();
593598
visited.push([
594599
'enter',
595600
node.kind,
596601
node.kind === 'Name' ? node.value : null,
597602
String(type),
603+
String(parentType),
598604
]);
599605
},
600606
leave(node) {
601607
const type = typeInfo.getInputType();
608+
const parentType = typeInfo.getParentInputType();
602609
visited.push([
603610
'leave',
604611
node.kind,
605612
node.kind === 'Name' ? node.value : null,
606613
String(type),
614+
String(parentType),
607615
]);
608616
},
609617
}),
@@ -613,12 +621,12 @@ describe('visitWithTypeInfo', () => {
613621
// Everything including ListValue should have type: undefined since the
614622
// contents of custom scalars are not part of the GraphQL type system.
615623
// ListValues carry the item type, so the item type is also undefined.
616-
['enter', 'ListValue', null, 'undefined'],
617-
['enter', 'FloatValue', null, 'undefined'],
618-
['leave', 'FloatValue', null, 'undefined'],
619-
['enter', 'FloatValue', null, 'undefined'],
620-
['leave', 'FloatValue', null, 'undefined'],
621-
['leave', 'ListValue', null, 'undefined'],
624+
['enter', 'ListValue', null, 'undefined', 'GeoPoint'],
625+
['enter', 'FloatValue', null, 'undefined', 'undefined'],
626+
['leave', 'FloatValue', null, 'undefined', 'undefined'],
627+
['enter', 'FloatValue', null, 'undefined', 'undefined'],
628+
['leave', 'FloatValue', null, 'undefined', 'undefined'],
629+
['leave', 'ListValue', null, 'undefined', 'GeoPoint'],
622630
]);
623631
});
624632

0 commit comments

Comments
 (0)