Skip to content

Commit c430347

Browse files
committed
test getParentInputType with complex custom scalars
getInputType => undefined getParentInputType => the custom scalar no matter how far out of the type system you go
1 parent 87e20de commit c430347

3 files changed

Lines changed: 36 additions & 20 deletions

File tree

src/utilities/TypeInfo.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ export class TypeInfo {
116116
return this._inputTypeStack.at(-1);
117117
}
118118

119+
// Note: continues to expose the closest enclosing valid input type if
120+
// traversal descends into syntax with no corresponding GraphQL input type.
119121
getParentInputType(): Maybe<GraphQLInputType> {
120122
return this._inputTypeStack.at(-2);
121123
}

src/utilities/__tests__/TypeInfo-test.ts

Lines changed: 32 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,22 @@ 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+
// getParentInputType() continues to report the closest enclosing valid
565+
// input type even after traversal leaves the GraphQL input type system.
566+
['enter', 'ObjectValue', null, 'GeoPoint', 'undefined'],
567+
['enter', 'ObjectField', null, 'undefined', 'GeoPoint'],
568+
['enter', 'Name', 'x', 'undefined', 'GeoPoint'],
569+
['leave', 'Name', 'x', 'undefined', 'GeoPoint'],
570+
['enter', 'FloatValue', null, 'undefined', 'GeoPoint'],
571+
['leave', 'FloatValue', null, 'undefined', 'GeoPoint'],
572+
['leave', 'ObjectField', null, 'undefined', 'GeoPoint'],
573+
['enter', 'ObjectField', null, 'undefined', 'GeoPoint'],
574+
['enter', 'Name', 'y', 'undefined', 'GeoPoint'],
575+
['leave', 'Name', 'y', 'undefined', 'GeoPoint'],
576+
['enter', 'FloatValue', null, 'undefined', 'GeoPoint'],
577+
['leave', 'FloatValue', null, 'undefined', 'GeoPoint'],
578+
['leave', 'ObjectField', null, 'undefined', 'GeoPoint'],
579+
['leave', 'ObjectValue', null, 'GeoPoint', 'undefined'],
574580
]);
575581
});
576582

@@ -590,20 +596,24 @@ describe('visitWithTypeInfo', () => {
590596
visitWithTypeInfo(typeInfo, {
591597
enter(node) {
592598
const type = typeInfo.getInputType();
599+
const parentType = typeInfo.getParentInputType();
593600
visited.push([
594601
'enter',
595602
node.kind,
596603
node.kind === 'Name' ? node.value : null,
597604
String(type),
605+
String(parentType),
598606
]);
599607
},
600608
leave(node) {
601609
const type = typeInfo.getInputType();
610+
const parentType = typeInfo.getParentInputType();
602611
visited.push([
603612
'leave',
604613
node.kind,
605614
node.kind === 'Name' ? node.value : null,
606615
String(type),
616+
String(parentType),
607617
]);
608618
},
609619
}),
@@ -613,12 +623,14 @@ describe('visitWithTypeInfo', () => {
613623
// Everything including ListValue should have type: undefined since the
614624
// contents of custom scalars are not part of the GraphQL type system.
615625
// 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'],
626+
// getParentInputType() continues to report the closest enclosing valid
627+
// input type even after traversal leaves the GraphQL input type system.
628+
['enter', 'ListValue', null, 'undefined', 'GeoPoint'],
629+
['enter', 'FloatValue', null, 'undefined', 'GeoPoint'],
630+
['leave', 'FloatValue', null, 'undefined', 'GeoPoint'],
631+
['enter', 'FloatValue', null, 'undefined', 'GeoPoint'],
632+
['leave', 'FloatValue', null, 'undefined', 'GeoPoint'],
633+
['leave', 'ListValue', null, 'undefined', 'GeoPoint'],
622634
]);
623635
});
624636

src/validation/ValidationContext.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,8 @@ export class ValidationContext extends ASTValidationContext {
285285
return this._typeInfo.getInputType();
286286
}
287287

288+
// Note: continues to expose the closest enclosing valid input type if
289+
// traversal descends into syntax with no corresponding GraphQL input type.
288290
getParentInputType(): Maybe<GraphQLInputType> {
289291
return this._typeInfo.getParentInputType();
290292
}

0 commit comments

Comments
 (0)