1818import graphql .schema .idl .errors .InterfaceFieldArgumentRedefinitionError ;
1919import graphql .schema .idl .errors .InterfaceFieldRedefinitionError ;
2020import graphql .schema .idl .errors .InterfaceImplementingItselfError ;
21+ import graphql .schema .idl .errors .InterfaceWithCircularImplementationHierarchyError ;
2122import graphql .schema .idl .errors .MissingInterfaceFieldArgumentsError ;
2223import graphql .schema .idl .errors .MissingInterfaceFieldError ;
2324import graphql .schema .idl .errors .MissingTransitiveInterfaceError ;
2425
2526import java .util .Collection ;
26- import java .util .HashSet ;
27+ import java .util .LinkedHashSet ;
2728import java .util .List ;
2829import java .util .Map ;
2930import java .util .Optional ;
3435import java .util .stream .Collectors ;
3536
3637import static java .util .stream .Collectors .toList ;
38+ import static java .util .stream .Collectors .toSet ;
3739
3840/**
3941 * A support class to help break up the large SchemaTypeChecker class. This handles
@@ -48,75 +50,82 @@ void checkImplementingTypes(List<GraphQLError> errors, TypeDefinitionRegistry ty
4850 // objects
4951 List <ObjectTypeDefinition > objectTypes = filterTo (typesMap , ObjectTypeDefinition .class );
5052 objectTypes .forEach (type -> {
51- checkImplementingType (errors , typeRegistry , type , "object" );
53+ checkImplementingType ("object" , errors , typeRegistry , type );
5254 });
5355
54-
5556 List <ObjectTypeExtensionDefinition > objectExtensions = typeRegistry .objectTypeExtensions ().values ()
5657 .stream ().flatMap (Collection ::stream ).collect (toList ());
5758
5859 objectExtensions .forEach (type -> {
59- checkImplementingType (errors , typeRegistry , type , "object extension" );
60+ checkImplementingType ("object extension" , errors , typeRegistry , type );
6061 });
6162
6263 // interfaces
6364 List <InterfaceTypeDefinition > interfacesTypes = filterTo (typesMap , InterfaceTypeDefinition .class );
6465 interfacesTypes .forEach (type -> {
65- checkImplementingType (errors , typeRegistry , type , "interface" );
66+ checkImplementingType ("interface" , errors , typeRegistry , type );
6667 });
6768
6869 List <InterfaceTypeExtensionDefinition > interfaceExtensions = typeRegistry .interfaceTypeExtensions ().values ()
6970 .stream ().flatMap (Collection ::stream ).collect (toList ());
7071
7172 interfaceExtensions .forEach (type -> {
72- checkImplementingType (errors , typeRegistry , type , "interface extension" );
73+ checkImplementingType ("interface extension" , errors , typeRegistry , type );
7374 });
7475 }
7576
7677 private void checkImplementingType (
78+ String typeOfType ,
7779 List <GraphQLError > errors ,
7880 TypeDefinitionRegistry typeRegistry ,
79- ImplementingTypeDefinition type ,
80- String typeName ) {
81+ ImplementingTypeDefinition type ) {
8182
8283 List <Type > implementsTypes = type .getImplements ();
8384
84- implementsTypes .forEach (checkInterfaceIsImplemented (typeName , typeRegistry , errors , type ));
85+ implementsTypes .forEach (checkInterfaceIsImplemented (typeOfType , typeRegistry , errors , type ));
86+
87+ Set <String > interfacesToImplement = findInterfacesToImplement (type , typeRegistry );
88+ Set <String > implementedInterfaces = findImplementedInterfaces (type , typeRegistry );
8589
8690 if (type instanceof InterfaceTypeDefinition ) {
87- final boolean implementItself = checkInterfaceDoesntImplementItself ( typeName , typeRegistry , errors , ( InterfaceTypeDefinition ) type , implementsTypes );
88- // Don't try to navigate the hierarchy if the interface implements itself, as this would result in an infinite loop
89- if (! implementItself ) {
90- checkTransitiveInterfacesAreDeclared ( typeName , typeRegistry , errors , type , implementsTypes );
91+ if (! checkInterfaceImplementItself ( typeOfType , errors , type , implementedInterfaces )) {
92+ if (! checkInterfaceHasCircularHierarchy ( typeOfType , errors , interfacesToImplement , type )) {
93+ checkTransitiveInterfacesAreDeclared ( typeOfType , errors , type , interfacesToImplement , implementedInterfaces );
94+ }
9195 }
9296 } else {
93- checkTransitiveInterfacesAreDeclared (typeName , typeRegistry , errors , type , implementsTypes );
97+ checkTransitiveInterfacesAreDeclared (typeOfType , errors , type , interfacesToImplement , implementedInterfaces );
9498 }
9599 }
96100
97- private boolean checkInterfaceDoesntImplementItself (
101+ private boolean checkInterfaceHasCircularHierarchy (
98102 String typeOfType ,
99- TypeDefinitionRegistry typeRegistry ,
100103 List <GraphQLError > errors ,
101- InterfaceTypeDefinition typeDefinition ,
102- List <Type > implementsInterfaces
103- ) {
104- Set <String > implementsInterfacesNames = implementsInterfaces .stream ()
105- .map (t -> toInterfaceTypeDefinition (t , typeRegistry ))
106- .filter (Optional ::isPresent )
107- .map (Optional ::get )
108- .map (TypeDefinition ::getName )
109- .collect (Collectors .toSet ());
104+ Set <String > interfacesToImplement ,
105+ ImplementingTypeDefinition type ) {
106+
107+ if (interfacesToImplement .contains (type .getName ())) {
108+ errors .add (new InterfaceWithCircularImplementationHierarchyError (typeOfType , type , interfacesToImplement ));
109+ return true ;
110+ }
111+
112+ return false ;
113+ }
110114
115+ private boolean checkInterfaceImplementItself (
116+ String typeOfType ,
117+ List <GraphQLError > errors ,
118+ ImplementingTypeDefinition typeDefinition ,
119+ Set <String > implementedInterfacesNames
120+ ) {
111121 String thisInterfaceName = typeDefinition .getName ();
112122
113- if (implementsInterfacesNames .contains (thisInterfaceName )) {
123+ if (implementedInterfacesNames .contains (thisInterfaceName )) {
114124 errors .add (new InterfaceImplementingItselfError (typeOfType , typeDefinition ));
115125 return true ;
116126 }
117127
118128 return false ;
119-
120129 }
121130
122131 private Consumer <? super Type > checkInterfaceIsImplemented (
@@ -161,27 +170,27 @@ private Consumer<? super Type> checkInterfaceIsImplemented(
161170 };
162171 }
163172
164- private void checkTransitiveInterfacesAreDeclared (
165- String typeOfType ,
166- TypeDefinitionRegistry typeRegistry ,
167- List <GraphQLError > errors ,
168- ImplementingTypeDefinition typeDefinition ,
169- List <Type > implementsInterfaces
170- ) {
171- Set <String > interfacesToBeImplemented = new HashSet <>();
172-
173- findImplementingInterfaces (typeDefinition , typeRegistry , interfacesToBeImplemented );
173+ private Set <String > findImplementedInterfaces (ImplementingTypeDefinition typeDefinition , TypeDefinitionRegistry typeRegistry ) {
174+ List <Type > implementz = typeDefinition .getImplements ();
174175
175- Set < String > implementsInterfacesNames = implementsInterfaces .stream ()
176+ return implementz .stream ()
176177 .map (t -> toInterfaceTypeDefinition (t , typeRegistry ))
177178 .filter (Optional ::isPresent )
178179 .map (Optional ::get )
179180 .map (TypeDefinition ::getName )
180- .collect (Collectors .toSet ());
181+ .collect (toSet ());
182+ }
181183
182- Set <String > missingInterfaces = interfacesToBeImplemented .stream ()
183- .filter (i -> !implementsInterfacesNames .contains (i ))
184- .collect (Collectors .toSet ());
184+ private void checkTransitiveInterfacesAreDeclared (
185+ String typeOfType ,
186+ List <GraphQLError > errors ,
187+ ImplementingTypeDefinition typeDefinition ,
188+ Set <String > interfaceHierarchy ,
189+ Set <String > implementedInterfaces
190+ ) {
191+ Set <String > missingInterfaces = interfaceHierarchy .stream ()
192+ .filter (i -> !implementedInterfaces .contains (i ))
193+ .collect (toSet ());
185194
186195 if (!missingInterfaces .isEmpty ()) {
187196 errors .add (new MissingTransitiveInterfaceError (typeOfType , typeDefinition , missingInterfaces ));
@@ -230,17 +239,31 @@ private <T> BinaryOperator<T> mergeFirstValue() {
230239 return (v1 , v2 ) -> v1 ;
231240 }
232241
233- private void findImplementingInterfaces (ImplementingTypeDefinition type , TypeDefinitionRegistry typeRegistry , Set < String > allInterfaces ) {
234- final List < Type > implementsTypes = type . getImplements ();
242+ private Set < String > findInterfacesToImplement (ImplementingTypeDefinition type , TypeDefinitionRegistry typeRegistry ) {
243+ Set < String > interfaceHierarchy = new LinkedHashSet <> ();
235244
236- implementsTypes .stream ()
237- .map (t -> toInterfaceTypeDefinition (t , typeRegistry ))
238- .filter (Optional ::isPresent )
239- .map (Optional ::get )
240- .forEach (t -> {
241- allInterfaces .add (t .getName ());
242- findImplementingInterfaces (t , typeRegistry , allInterfaces );
243- });
245+ List <Type > implementsTypes = type .getImplements ();
246+
247+ Set <InterfaceTypeDefinition > interfaces = toInterfaceTypeDefinitions (typeRegistry , implementsTypes );
248+
249+ findInterfacesToImplement (type , interfaces , typeRegistry , interfaceHierarchy );
250+
251+ return interfaceHierarchy ;
252+ }
253+
254+
255+ private void findInterfacesToImplement (ImplementingTypeDefinition rootType , Set <InterfaceTypeDefinition > interfaces , TypeDefinitionRegistry typeRegistry , Set <String > allInterfaces ) {
256+ allInterfaces .addAll (interfaces .stream ().map (InterfaceTypeDefinition ::getName ).collect (toSet ()));
257+
258+ if (allInterfaces .contains (rootType .getName ())) {
259+ return ;
260+ }
261+
262+ interfaces .stream ().map (InterfaceTypeDefinition ::getImplements ).forEach (i -> {
263+ Set <InterfaceTypeDefinition > innerInterface = toInterfaceTypeDefinitions (typeRegistry , i );
264+
265+ findInterfacesToImplement (rootType , innerInterface , typeRegistry , allInterfaces );
266+ });
244267 }
245268
246269 private Optional <InterfaceTypeDefinition > toInterfaceTypeDefinition (Type type , TypeDefinitionRegistry typeRegistry ) {
@@ -250,4 +273,13 @@ private Optional<InterfaceTypeDefinition> toInterfaceTypeDefinition(Type type, T
250273 return typeRegistry .getType (unwrapped , InterfaceTypeDefinition .class );
251274 }
252275
276+ private Set <InterfaceTypeDefinition > toInterfaceTypeDefinitions (TypeDefinitionRegistry typeRegistry , List <Type > implementsTypes ) {
277+ return implementsTypes .stream ()
278+ .map (t -> toInterfaceTypeDefinition (t , typeRegistry ))
279+ .filter (Optional ::isPresent )
280+ .map (Optional ::get )
281+ .collect (toSet ());
282+ }
283+
284+
253285}
0 commit comments