Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions spring-graphql-docs/modules/ROOT/pages/federation.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -81,6 +82,8 @@ public final class FederationSchemaFactory

private @Nullable TypeResolver typeResolver;

private final List<HandlerMethodArgumentResolver> customArgumentResolvers = new ArrayList<>(8);

private final Map<String, EntityHandlerMethod> handlerMethods = new LinkedHashMap<>();


Expand All @@ -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();
Expand Down Expand Up @@ -140,6 +155,8 @@ protected HandlerMethodArgumentResolverComposite initArgumentResolvers() {
resolvers.addResolver(new ContinuationHandlerMethodArgumentResolver());
}

this.customArgumentResolvers.forEach(resolvers::addResolver);

return resolvers;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -99,6 +101,17 @@ void fetchEntities() {
assertAuthor(2, "Douglas", "Adams", helper);
}

@Test // gh-1567
void customArgumentResolver() {
Map<String, Object> 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<String, Object> variables =
Expand Down Expand Up @@ -202,9 +215,13 @@ void unmappedEntity() {
.withMessage("Unmapped entity types: 'Media', 'PrintedMedia', 'Book'");
}

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

ExecutionGraphQlRequest request = TestExecutionRequest.forDocumentAndVars(document, variables);
Mono<ExecutionGraphQlResponse> responseMono = graphQlService(controllerClass).execute(request);
Mono<ExecutionGraphQlResponse> responseMono =
graphQlService(controllerClass, customArgumentResolvers).execute(request);
return ResponseHelper.forResponse(responseMono);
}

Expand All @@ -222,7 +239,8 @@ private static void assertError(ResponseHelper helper, int i, String errorType,
assertThat(helper.<Object>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();
Expand All @@ -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)
Expand All @@ -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 {
Expand Down