diff --git a/spring-graphql-docs/modules/ROOT/pages/federation.adoc b/spring-graphql-docs/modules/ROOT/pages/federation.adoc index 40bfad3e0..334177124 100644 --- a/spring-graphql-docs/modules/ROOT/pages/federation.adoc +++ b/spring-graphql-docs/modules/ROOT/pages/federation.adoc @@ -141,6 +141,10 @@ You can load federated entities with a `DataLoader`: Entity mapping methods support the following arguments: +To resolve additional argument types, register a `HandlerMethodArgumentResolver` with the +`FederationSchemaFactory` through `addCustomArgumentResolver` before factory initialization. +Custom resolvers are ordered after built-in resolvers. + [cols="1,2"] |=== | Method Argument | Description 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 bf629ca2a..0ad6aff8b 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 @@ -49,6 +49,7 @@ import org.springframework.core.annotation.AnnotatedElementUtils; import org.springframework.graphql.data.GraphQlArgumentBinder; import org.springframework.graphql.data.method.HandlerMethod; +import org.springframework.graphql.data.method.HandlerMethodArgumentResolver; import org.springframework.graphql.data.method.HandlerMethodArgumentResolverComposite; import org.springframework.graphql.data.method.annotation.support.AnnotatedControllerDetectionSupport; import org.springframework.graphql.data.method.annotation.support.AuthenticationPrincipalArgumentResolver; @@ -81,6 +82,8 @@ public final class FederationSchemaFactory private @Nullable TypeResolver typeResolver; + private final List customArgumentResolvers = new ArrayList<>(8); + private final Map handlerMethods = new LinkedHashMap<>(); @@ -95,6 +98,18 @@ public void setTypeResolver(@Nullable TypeResolver typeResolver) { } + /** + * Add a {@link HandlerMethodArgumentResolver} for custom entity handler + * method arguments. Such custom resolvers are ordered after built-in resolvers. + * Call this before {@link #afterPropertiesSet()}. + * @param resolver the resolver to add + * @since 2.1.1 + */ + public void addCustomArgumentResolver(HandlerMethodArgumentResolver resolver) { + this.customArgumentResolvers.add(resolver); + } + + @Override public void afterPropertiesSet() { super.afterPropertiesSet(); @@ -140,6 +155,8 @@ protected HandlerMethodArgumentResolverComposite initArgumentResolvers() { resolvers.addResolver(new ContinuationHandlerMethodArgumentResolver()); } + this.customArgumentResolvers.forEach(resolvers::addResolver); + return resolvers; } 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 62fada1f8..4ccdec526 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 @@ -34,6 +34,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.core.MethodParameter; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.graphql.Author; @@ -46,6 +47,7 @@ import org.springframework.graphql.ResponseHelper; import org.springframework.graphql.TestExecutionGraphQlService; import org.springframework.graphql.TestExecutionRequest; +import org.springframework.graphql.data.method.HandlerMethodArgumentResolver; import org.springframework.graphql.data.method.annotation.Argument; import org.springframework.graphql.data.method.annotation.BatchMapping; import org.springframework.graphql.data.method.annotation.GraphQlExceptionHandler; @@ -99,6 +101,17 @@ void fetchEntities() { assertAuthor(2, "Douglas", "Adams", helper); } + @Test // gh-1567 + void customArgumentResolver() { + Map variables = Map.of("representations", List.of( + Map.of("__typename", "Book", "id", "3"))); + + ResponseHelper helper = executeWith(CustomArgumentResolverController.class, variables, + new CustomEntityArgumentResolver()); + + assertAuthor(0, "Joseph", "Heller", helper); + } + @Test void fetchEntitiesWithExceptions() { Map variables = @@ -202,9 +215,13 @@ void unmappedEntity() { .withMessage("Unmapped entity types: 'Media', 'PrintedMedia', 'Book'"); } - private static ResponseHelper executeWith(Class controllerClass, Map variables) { + private static ResponseHelper executeWith( + Class controllerClass, Map variables, + HandlerMethodArgumentResolver... customArgumentResolvers) { + ExecutionGraphQlRequest request = TestExecutionRequest.forDocumentAndVars(document, variables); - Mono responseMono = graphQlService(controllerClass).execute(request); + Mono responseMono = + graphQlService(controllerClass, customArgumentResolvers).execute(request); return ResponseHelper.forResponse(responseMono); } @@ -222,7 +239,8 @@ private static void assertError(ResponseHelper helper, int i, String errorType, assertThat(helper.rawValue(path)).isNull(); } - private static TestExecutionGraphQlService graphQlService(Class controllerClass) { + private static TestExecutionGraphQlService graphQlService( + Class controllerClass, HandlerMethodArgumentResolver... customArgumentResolvers) { BatchLoaderRegistry registry = new DefaultBatchLoaderRegistry(); AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); @@ -236,6 +254,9 @@ private static TestExecutionGraphQlService graphQlService(Class controllerCla FederationSchemaFactory schemaFactory = new FederationSchemaFactory(); schemaFactory.setApplicationContext(context); + for (HandlerMethodArgumentResolver resolver : customArgumentResolvers) { + schemaFactory.addCustomArgumentResolver(resolver); + } schemaFactory.afterPropertiesSet(); return GraphQlSetup.schemaResource(federationSchema) @@ -246,6 +267,36 @@ private static TestExecutionGraphQlService graphQlService(Class controllerCla } + private record CustomEntityArgument(String marker) { + } + + + private static class CustomEntityArgumentResolver implements HandlerMethodArgumentResolver { + + @Override + public boolean supportsParameter(MethodParameter parameter) { + return parameter.getParameterType() == CustomEntityArgument.class; + } + + @Override + public @Nullable Object resolveArgument(MethodParameter parameter, DataFetchingEnvironment environment) { + return new CustomEntityArgument("custom"); + } + } + + + @SuppressWarnings("unused") + @Controller + private static class CustomArgumentResolverController { + + @EntityMapping("Media") + public Book book(CustomEntityArgument argument) { + assertThat(argument.marker()).isEqualTo("custom"); + return BookSource.getBook(3L); + } + } + + @SuppressWarnings("unused") @Controller private static class BookController {