|
17 | 17 | import graphql.schema.idl.errors.InterfaceFieldArgumentNotOptionalError; |
18 | 18 | import graphql.schema.idl.errors.InterfaceFieldArgumentRedefinitionError; |
19 | 19 | import graphql.schema.idl.errors.InterfaceFieldRedefinitionError; |
| 20 | +import graphql.schema.idl.errors.InterfaceImplementingItselfError; |
20 | 21 | import graphql.schema.idl.errors.MissingInterfaceFieldArgumentsError; |
21 | 22 | import graphql.schema.idl.errors.MissingInterfaceFieldError; |
22 | 23 | import graphql.schema.idl.errors.MissingTransitiveInterfaceError; |
@@ -79,13 +80,51 @@ private void checkImplementingType( |
79 | 80 | String typeName) { |
80 | 81 |
|
81 | 82 | List<Type> implementsTypes = type.getImplements(); |
| 83 | + |
82 | 84 | implementsTypes.forEach(checkInterfaceIsImplemented(typeName, typeRegistry, errors, type)); |
83 | 85 |
|
84 | | - checkTransitiveInterfacesAreDeclared(typeName, typeRegistry, errors, type, implementsTypes); |
| 86 | + 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 | + } |
| 92 | + } else { |
| 93 | + checkTransitiveInterfacesAreDeclared(typeName, typeRegistry, errors, type, implementsTypes); |
| 94 | + } |
85 | 95 | } |
86 | 96 |
|
| 97 | + private boolean checkInterfaceDoesntImplementItself( |
| 98 | + String typeOfType, |
| 99 | + TypeDefinitionRegistry typeRegistry, |
| 100 | + 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()); |
| 110 | + |
| 111 | + String thisInterfaceName = typeDefinition.getName(); |
| 112 | + |
| 113 | + if (implementsInterfacesNames.contains(thisInterfaceName)) { |
| 114 | + errors.add(new InterfaceImplementingItselfError(typeOfType, typeDefinition)); |
| 115 | + return true; |
| 116 | + } |
| 117 | + |
| 118 | + return false; |
| 119 | + |
| 120 | + } |
87 | 121 |
|
88 | | - private Consumer<? super Type> checkInterfaceIsImplemented(String typeOfType, TypeDefinitionRegistry typeRegistry, List<GraphQLError> errors, ImplementingTypeDefinition typeDefinition) { |
| 122 | + private Consumer<? super Type> checkInterfaceIsImplemented( |
| 123 | + String typeOfType, |
| 124 | + TypeDefinitionRegistry typeRegistry, |
| 125 | + List<GraphQLError> errors, |
| 126 | + ImplementingTypeDefinition typeDefinition |
| 127 | + ) { |
89 | 128 | return t -> { |
90 | 129 | List<FieldDefinition> fieldDefinitions = typeDefinition.getFieldDefinitions(); |
91 | 130 |
|
@@ -149,7 +188,14 @@ private void checkTransitiveInterfacesAreDeclared( |
149 | 188 | } |
150 | 189 | } |
151 | 190 |
|
152 | | - private void checkArgumentConsistency(String typeOfType, TypeDefinition objectTypeDef, InterfaceTypeDefinition interfaceTypeDef, FieldDefinition objectFieldDef, FieldDefinition interfaceFieldDef, List<GraphQLError> errors) { |
| 191 | + private void checkArgumentConsistency( |
| 192 | + String typeOfType, |
| 193 | + ImplementingTypeDefinition objectTypeDef, |
| 194 | + InterfaceTypeDefinition interfaceTypeDef, |
| 195 | + FieldDefinition objectFieldDef, |
| 196 | + FieldDefinition interfaceFieldDef, |
| 197 | + List<GraphQLError> errors |
| 198 | + ) { |
153 | 199 | List<InputValueDefinition> objectArgs = objectFieldDef.getInputValueDefinitions(); |
154 | 200 | List<InputValueDefinition> interfaceArgs = interfaceFieldDef.getInputValueDefinitions(); |
155 | 201 | for (int i = 0; i < interfaceArgs.size(); i++) { |
|
0 commit comments