Skip to content

Support multiple typeNames on @SchemaMapping and @BatchMapping #236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,10 @@
*/
package org.springframework.graphql.data.method.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.core.annotation.AliasFor;

import java.lang.annotation.*;

/**
* Annotation for a handler method that batch loads field values, given a list
* of source/parent values. For example:
Expand Down Expand Up @@ -93,6 +89,6 @@
* {@link SchemaMapping @SchemaMapping}. When used on both levels, the one
* here overrides the one at the class level.
*/
String typeName() default "";
String[] typeNames() default {};

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@SchemaMapping(typeName = "Mutation")
@SchemaMapping(typeNames = "Mutation")
public @interface MutationMapping {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@SchemaMapping(typeName = "Query")
@SchemaMapping(typeNames = "Query")
public @interface QueryMapping {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,11 @@
*/
package org.springframework.graphql.data.method.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import graphql.schema.DataFetchingEnvironment;

import org.springframework.core.annotation.AliasFor;

import java.lang.annotation.*;

/**
* Annotation to express the mapping of a handler method to a GraphQL type and
* field pair.
Expand Down Expand Up @@ -63,6 +58,6 @@
* When used on both levels, the one on the method level overrides the one
* at the class level.
*/
String typeName() default "";
String[] typeNames() default {};

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@SchemaMapping(typeName = "Subscription")
@SchemaMapping(typeNames = "Subscription")
public @interface SubscriptionMapping {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,13 @@
*/
package org.springframework.graphql.data.method.annotation.support;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import javax.validation.Validator;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import graphql.schema.FieldCoordinates;
import graphql.schema.idl.RuntimeWiring;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dataloader.DataLoader;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
Expand All @@ -61,6 +44,15 @@
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import javax.validation.Validator;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.*;
import java.util.stream.Collectors;

/**
* {@link RuntimeWiringConfigurer} that detects {@link SchemaMapping @SchemaMapping}
Expand Down Expand Up @@ -225,10 +217,10 @@ private Collection<MappingInfo> findHandlerMethods(Object handler, @Nullable Cla
}

Class<?> userClass = ClassUtils.getUserClass(handlerClass);
Map<Method, MappingInfo> map =
Map<Method, Collection<MappingInfo>> map =
MethodIntrospector.selectMethods(userClass, (Method method) -> getMappingInfo(method, handler, userClass));

Collection<MappingInfo> mappingInfos = map.values();
Collection<MappingInfo> mappingInfos = map.values().stream().flatMap(Collection::stream).collect(Collectors.toList());

if (logger.isTraceEnabled() && !mappingInfos.isEmpty()) {
logger.trace(formatMappings(userClass, mappingInfos));
Expand All @@ -238,7 +230,7 @@ private Collection<MappingInfo> findHandlerMethods(Object handler, @Nullable Cla
}

@Nullable
private MappingInfo getMappingInfo(Method method, Object handler, Class<?> handlerType) {
private Collection<MappingInfo> getMappingInfo(Method method, Object handler, Class<?> handlerType) {

Set<Annotation> annotations = AnnotatedElementUtils.findAllMergedAnnotations(
method, new LinkedHashSet<>(Arrays.asList(BatchMapping.class, SchemaMapping.class)));
Expand All @@ -252,55 +244,56 @@ private MappingInfo getMappingInfo(Method method, Object handler, Class<?> handl
"Expected either @BatchMapping or @SchemaMapping, not both: " + method.toGenericString());
}

String typeName;
String[] typeNames;
String field;
boolean batchMapping = false;
HandlerMethod handlerMethod = createHandlerMethod(method, handler, handlerType);

Annotation annotation = annotations.iterator().next();
if (annotation instanceof SchemaMapping) {
SchemaMapping mapping = (SchemaMapping) annotation;
typeName = mapping.typeName();
typeNames = mapping.typeNames();
field = (StringUtils.hasText(mapping.field()) ? mapping.field() : method.getName());
}
else {
BatchMapping mapping = (BatchMapping) annotation;
typeName = mapping.typeName();
typeNames = mapping.typeNames();
field = (StringUtils.hasText(mapping.field()) ? mapping.field() : method.getName());
batchMapping = true;
}

if (!StringUtils.hasText(typeName)) {
if (typeNames.length == 0) {
SchemaMapping mapping = AnnotatedElementUtils.findMergedAnnotation(handlerType, SchemaMapping.class);
if (mapping != null) {
typeName = mapping.typeName();
typeNames = mapping.typeNames();
}
}

if (!StringUtils.hasText(typeName)) {
if (typeNames.length == 0) {
for (MethodParameter parameter : handlerMethod.getMethodParameters()) {
if (!batchMapping) {
Assert.state(this.argumentResolvers != null, "`argumentResolvers` is not initialized");
HandlerMethodArgumentResolver resolver = this.argumentResolvers.getArgumentResolver(parameter);
if (resolver instanceof SourceMethodArgumentResolver) {
typeName = parameter.getParameterType().getSimpleName();
typeNames = new String[]{ parameter.getParameterType().getSimpleName() };
break;
}
}
else {
if (Collection.class.isAssignableFrom(parameter.getParameterType())) {
typeName = parameter.nested().getNestedParameterType().getSimpleName();
typeNames = new String[]{ parameter.nested().getNestedParameterType().getSimpleName() };
break;
}
}
}
}

Assert.hasText(typeName,
Assert.notEmpty(typeNames,
"No parentType specified, and a source/parent method argument was also not found: " +
handlerMethod.getShortLogMessage());

return new MappingInfo(typeName, field, batchMapping, handlerMethod);
final boolean finalBatchMapping = batchMapping;
return Arrays.stream(typeNames).map(type -> new MappingInfo(type, field, finalBatchMapping, handlerMethod)).collect(Collectors.toList());
}

private HandlerMethod createHandlerMethod(Method method, Object handler, Class<?> handlerType) {
Expand Down