55
66import graphql .PublicApi ;
77import graphql .schema .GraphQLFieldDefinition ;
8+ import graphql .schema .GraphQLInputObjectField ;
89import graphql .schema .GraphQLInterfaceType ;
10+ import graphql .schema .GraphQLNamedSchemaElement ;
911import graphql .schema .GraphQLObjectType ;
1012import graphql .schema .GraphQLSchema ;
1113import graphql .schema .GraphQLSchemaElement ;
@@ -84,7 +86,10 @@ private FieldVisibilityVisitor(VisibleFieldPredicate visibilityPredicate,
8486 @ Override
8587 public TraversalControl visitGraphQLInterfaceType (GraphQLInterfaceType node ,
8688 TraverserContext <GraphQLSchemaElement > context ) {
87- observedTypes .add (node );
89+ if (context .getBreadcrumbs ().stream ()
90+ .noneMatch (crumb -> crumb .getLocation ().getName ().equalsIgnoreCase ("addTypes" ))) {
91+ observedTypes .add (node );
92+ }
8893
8994 return TraversalControl .CONTINUE ;
9095 }
@@ -94,23 +99,49 @@ public TraversalControl visitGraphQLObjectType(GraphQLObjectType node,
9499 TraverserContext <GraphQLSchemaElement > context ) {
95100
96101 if (context .getBreadcrumbs ().stream ()
97- .anyMatch (crumb -> crumb .getLocation ().getName ().equalsIgnoreCase ("addTypes" ))) {
98- return TraversalControl . ABORT ;
102+ .noneMatch (crumb -> crumb .getLocation ().getName ().equalsIgnoreCase ("addTypes" ))) {
103+ observedTypes . add ( node ) ;
99104 }
100105
101- observedTypes .add (node );
102-
103106 return TraversalControl .CONTINUE ;
104107 }
105108
106109 @ Override
107110 public TraversalControl visitGraphQLFieldDefinition (GraphQLFieldDefinition definition ,
108111 TraverserContext <GraphQLSchemaElement > context ) {
112+ return visitField (definition , context );
113+ }
114+
115+ @ Override
116+ public TraversalControl visitGraphQLInputObjectField (GraphQLInputObjectField definition ,
117+ TraverserContext <GraphQLSchemaElement > context ) {
118+ return visitField (definition , context );
119+ }
120+
121+ @ Override
122+ public TraversalControl visitBackRef (TraverserContext <GraphQLSchemaElement > context ) {
123+ if (context .thisNode () instanceof GraphQLInterfaceType || context .thisNode () instanceof GraphQLObjectType ) {
124+ if (context .getBreadcrumbs ().stream ()
125+ .noneMatch (crumb -> crumb .getLocation ().getName ().equalsIgnoreCase ("addTypes" ))) {
126+ observedTypes .add ((GraphQLType ) context .thisNode ());
127+ }
128+ }
129+ return TraversalControl .CONTINUE ;
130+ }
131+
132+ private TraversalControl visitField (GraphQLNamedSchemaElement element ,
133+ TraverserContext <GraphQLSchemaElement > context ) {
134+
109135 VisibleFieldPredicateEnvironment environment = new VisibleFieldPredicateEnvironmentImpl (
110- definition , context .getParentNode ());
136+ element , context .getParentNode ());
111137 if (!visibilityPredicate .isVisible (environment )) {
112138 deleteNode (context );
113- removedTypes .add (definition .getType ());
139+
140+ if (element instanceof GraphQLFieldDefinition ) {
141+ removedTypes .add (((GraphQLFieldDefinition ) element ).getType ());
142+ } else if (element instanceof GraphQLInputObjectField ) {
143+ removedTypes .add (((GraphQLInputObjectField ) element ).getType ());
144+ }
114145 }
115146
116147 return TraversalControl .CONTINUE ;
@@ -131,10 +162,17 @@ private TypeVisibilityVisitor(Set<String> protectedTypeNames,
131162 this .removedTypes = removedTypes ;
132163 }
133164
165+ @ Override
166+ public TraversalControl visitGraphQLInterfaceType (GraphQLInterfaceType node ,
167+ TraverserContext <GraphQLSchemaElement > context ) {
168+ return super .visitGraphQLInterfaceType (node , context );
169+ }
170+
134171 @ Override
135172 public TraversalControl visitGraphQLObjectType (GraphQLObjectType node ,
136173 TraverserContext <GraphQLSchemaElement > context ) {
137174 if (!observedTypes .contains (node ) &&
175+ node .getInterfaces ().stream ().noneMatch (observedTypes ::contains ) &&
138176 node .getInterfaces ().stream ().anyMatch (removedTypes ::contains ) &&
139177 !ScalarInfo .isStandardScalar (node .getName ()) &&
140178 !protectedTypeNames .contains (node .getName ())) {
0 commit comments