Skip to content

Commit b0862fb

Browse files
committed
Polishing in FederationSchemaFactory
See gh-1088
1 parent 2f1c4b1 commit b0862fb

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

spring-graphql/src/main/java/org/springframework/graphql/data/federation/FederationSchemaFactory.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.graphql.data.federation;
1818

1919
import java.lang.reflect.Method;
20+
import java.util.ArrayList;
2021
import java.util.LinkedHashMap;
2122
import java.util.List;
2223
import java.util.Map;
@@ -67,7 +68,6 @@
6768
* @author Rossen Stoyanchev
6869
* @since 1.3.0
6970
* @see Federation#transform(TypeDefinitionRegistry, RuntimeWiring)
70-
*
7171
*/
7272
public final class FederationSchemaFactory
7373
extends AnnotatedControllerDetectionSupport<FederationSchemaFactory.EntityMappingInfo> {
@@ -80,7 +80,7 @@ public final class FederationSchemaFactory
8080

8181
/**
8282
* Configure a resolver that helps to map Java to entity schema type names.
83-
* <p>By default this is {@link ClassNameTypeResolver}.
83+
* <p>By default, this is {@link ClassNameTypeResolver}.
8484
* @param typeResolver the custom type resolver to use
8585
* @see SchemaTransformer#resolveEntityType(TypeResolver)
8686
*/
@@ -186,13 +186,19 @@ public SchemaTransformer createSchemaTransformer(TypeDefinitionRegistry registry
186186
}
187187

188188
private void checkEntityMappings(TypeDefinitionRegistry registry) {
189+
List<String> unmappedEntities = new ArrayList<>();
189190
for (TypeDefinition<?> type : registry.types().values()) {
190191
type.getDirectives().forEach((directive) -> {
191192
boolean isEntityType = directive.getName().equalsIgnoreCase("key");
192-
Assert.state(!isEntityType || this.handlerMethods.containsKey(type.getName()),
193-
"No EntityMapping method for federated type: '" + type.getName() + "'");
193+
if (isEntityType && !this.handlerMethods.containsKey(type.getName())) {
194+
unmappedEntities.add(type.getName());
195+
}
194196
});
195197
}
198+
if (!unmappedEntities.isEmpty()) {
199+
throw new IllegalStateException("Unmapped entity types: " +
200+
unmappedEntities.stream().collect(Collectors.joining("', '", "'", "'")));
201+
}
196202
}
197203

198204

spring-graphql/src/test/java/org/springframework/graphql/data/federation/EntityMappingInvocationTests.java

+2-8
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,8 @@ void batchingWithoutResult(Class<?> controllerClass) {
178178

179179
@Test
180180
void unmappedEntity() {
181-
Map<String, Object> variables =
182-
Map.of("representations", List.of(
183-
Map.of("__typename", "Book", "id", "-99"),
184-
Map.of("__typename", "Book", "id", "4"),
185-
Map.of("__typename", "Book", "id", "5")));
186-
187-
assertThatIllegalStateException().isThrownBy(() -> executeWith(EmptyController.class, variables))
188-
.withMessage("No EntityMapping method for federated type: 'Book'");
181+
assertThatIllegalStateException().isThrownBy(() -> executeWith(EmptyController.class, Map.of()))
182+
.withMessage("Unmapped entity types: 'Book'");
189183
}
190184

191185
private static ResponseHelper executeWith(Class<?> controllerClass, Map<String, Object> variables) {

0 commit comments

Comments
 (0)