use of org.springframework.graphql.data.method.HandlerMethodArgumentResolver in project spring-graphql by spring-projects.
the class AnnotatedControllerConfigurer method getMappingInfo.
@Nullable
private MappingInfo getMappingInfo(Method method, Object handler, Class<?> handlerType) {
Set<Annotation> annotations = AnnotatedElementUtils.findAllMergedAnnotations(method, new LinkedHashSet<>(Arrays.asList(BatchMapping.class, SchemaMapping.class)));
if (annotations.isEmpty()) {
return null;
}
if (annotations.size() != 1) {
throw new IllegalArgumentException("Expected either @BatchMapping or @SchemaMapping, not both: " + method.toGenericString());
}
String typeName;
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();
field = (StringUtils.hasText(mapping.field()) ? mapping.field() : method.getName());
} else {
BatchMapping mapping = (BatchMapping) annotation;
typeName = mapping.typeName();
field = (StringUtils.hasText(mapping.field()) ? mapping.field() : method.getName());
batchMapping = true;
}
if (!StringUtils.hasText(typeName)) {
SchemaMapping mapping = AnnotatedElementUtils.findMergedAnnotation(handlerType, SchemaMapping.class);
if (mapping != null) {
typeName = mapping.typeName();
}
}
if (!StringUtils.hasText(typeName)) {
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();
break;
}
} else {
if (Collection.class.isAssignableFrom(parameter.getParameterType())) {
typeName = parameter.nested().getNestedParameterType().getSimpleName();
break;
}
}
}
}
Assert.hasText(typeName, "No parentType specified, and a source/parent method argument was also not found: " + handlerMethod.getShortLogMessage());
return new MappingInfo(typeName, field, batchMapping, handlerMethod);
}
Aggregations