From b0862fbaf4d7fb88c6e2506ed70d3bdbf8a42b1f Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Thu, 20 Feb 2025 11:52:51 +0000 Subject: [PATCH] Polishing in FederationSchemaFactory See gh-1088 --- .../data/federation/FederationSchemaFactory.java | 14 ++++++++++---- .../federation/EntityMappingInvocationTests.java | 10 ++-------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/spring-graphql/src/main/java/org/springframework/graphql/data/federation/FederationSchemaFactory.java b/spring-graphql/src/main/java/org/springframework/graphql/data/federation/FederationSchemaFactory.java index 269d7731..f3495838 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/data/federation/FederationSchemaFactory.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/data/federation/FederationSchemaFactory.java @@ -17,6 +17,7 @@ package org.springframework.graphql.data.federation; import java.lang.reflect.Method; +import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -67,7 +68,6 @@ import org.springframework.util.StringUtils; * @author Rossen Stoyanchev * @since 1.3.0 * @see Federation#transform(TypeDefinitionRegistry, RuntimeWiring) - * */ public final class FederationSchemaFactory extends AnnotatedControllerDetectionSupport { @@ -80,7 +80,7 @@ public final class FederationSchemaFactory /** * Configure a resolver that helps to map Java to entity schema type names. - *

By default this is {@link ClassNameTypeResolver}. + *

By default, this is {@link ClassNameTypeResolver}. * @param typeResolver the custom type resolver to use * @see SchemaTransformer#resolveEntityType(TypeResolver) */ @@ -186,13 +186,19 @@ public final class FederationSchemaFactory } private void checkEntityMappings(TypeDefinitionRegistry registry) { + List unmappedEntities = new ArrayList<>(); for (TypeDefinition type : registry.types().values()) { type.getDirectives().forEach((directive) -> { boolean isEntityType = directive.getName().equalsIgnoreCase("key"); - Assert.state(!isEntityType || this.handlerMethods.containsKey(type.getName()), - "No EntityMapping method for federated type: '" + type.getName() + "'"); + if (isEntityType && !this.handlerMethods.containsKey(type.getName())) { + unmappedEntities.add(type.getName()); + } }); } + if (!unmappedEntities.isEmpty()) { + throw new IllegalStateException("Unmapped entity types: " + + unmappedEntities.stream().collect(Collectors.joining("', '", "'", "'"))); + } } diff --git a/spring-graphql/src/test/java/org/springframework/graphql/data/federation/EntityMappingInvocationTests.java b/spring-graphql/src/test/java/org/springframework/graphql/data/federation/EntityMappingInvocationTests.java index 0f26ad20..08b60768 100644 --- a/spring-graphql/src/test/java/org/springframework/graphql/data/federation/EntityMappingInvocationTests.java +++ b/spring-graphql/src/test/java/org/springframework/graphql/data/federation/EntityMappingInvocationTests.java @@ -178,14 +178,8 @@ public class EntityMappingInvocationTests { @Test void unmappedEntity() { - Map variables = - Map.of("representations", List.of( - Map.of("__typename", "Book", "id", "-99"), - Map.of("__typename", "Book", "id", "4"), - Map.of("__typename", "Book", "id", "5"))); - - assertThatIllegalStateException().isThrownBy(() -> executeWith(EmptyController.class, variables)) - .withMessage("No EntityMapping method for federated type: 'Book'"); + assertThatIllegalStateException().isThrownBy(() -> executeWith(EmptyController.class, Map.of())) + .withMessage("Unmapped entity types: 'Book'"); } private static ResponseHelper executeWith(Class controllerClass, Map variables) {