Search in sources :

Example 1 with HandlerMethodArgumentResolver

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);
}
Also used : BatchMapping(org.springframework.graphql.data.method.annotation.BatchMapping) SchemaMapping(org.springframework.graphql.data.method.annotation.SchemaMapping) HandlerMethodArgumentResolver(org.springframework.graphql.data.method.HandlerMethodArgumentResolver) Annotation(java.lang.annotation.Annotation) HandlerMethod(org.springframework.graphql.data.method.HandlerMethod) MethodParameter(org.springframework.core.MethodParameter) Nullable(org.springframework.lang.Nullable)

Aggregations

Annotation (java.lang.annotation.Annotation)1 MethodParameter (org.springframework.core.MethodParameter)1 HandlerMethod (org.springframework.graphql.data.method.HandlerMethod)1 HandlerMethodArgumentResolver (org.springframework.graphql.data.method.HandlerMethodArgumentResolver)1 BatchMapping (org.springframework.graphql.data.method.annotation.BatchMapping)1 SchemaMapping (org.springframework.graphql.data.method.annotation.SchemaMapping)1 Nullable (org.springframework.lang.Nullable)1