Search in sources :

Example 1 with HandlerMethod

use of org.springframework.graphql.data.method.HandlerMethod in project spring-graphql by spring-projects.

the class HandlerMethodInputValidatorTests method shouldRaiseValidationErrorForAnnotatedParams.

@Test
void shouldRaiseValidationErrorForAnnotatedParams() throws Exception {
    HandlerMethod method = findHandlerMethod(MyValidBean.class, "myValidMethod");
    assertViolations(() -> validator.validate(method, new Object[] { null, 2 })).anyMatch(violation -> violation.getPropertyPath().toString().equals("myValidMethod.arg0"));
    assertViolations(() -> validator.validate(method, new Object[] { "test", 12 })).anyMatch(violation -> violation.getPropertyPath().toString().equals("myValidMethod.arg1"));
}
Also used : HandlerMethod(org.springframework.graphql.data.method.HandlerMethod) Test(org.junit.jupiter.api.Test)

Example 2 with HandlerMethod

use of org.springframework.graphql.data.method.HandlerMethod in project spring-graphql by spring-projects.

the class HandlerMethodInputValidatorTests method shouldRaiseValidationErrorForAnnotatedParamsWithGroups.

@Test
void shouldRaiseValidationErrorForAnnotatedParamsWithGroups() throws Exception {
    HandlerMethod myValidMethodWithGroup = findHandlerMethod(MyValidBeanWithGroup.class, "myValidMethodWithGroup");
    assertViolations(() -> validator.validate(myValidMethodWithGroup, new Object[] { null })).anyMatch(violation -> violation.getPropertyPath().toString().equals("myValidMethodWithGroup.arg0"));
    HandlerMethod myValidMethodWithGroupOnType = findHandlerMethod(MyValidBeanWithGroup.class, "myValidMethodWithGroupOnType");
    assertViolations(() -> validator.validate(myValidMethodWithGroupOnType, new Object[] { null })).anyMatch(violation -> violation.getPropertyPath().toString().equals("myValidMethodWithGroupOnType.arg0"));
}
Also used : HandlerMethod(org.springframework.graphql.data.method.HandlerMethod) Test(org.junit.jupiter.api.Test)

Example 3 with HandlerMethod

use of org.springframework.graphql.data.method.HandlerMethod in project spring-graphql by spring-projects.

the class AnnotatedControllerConfigurer method findHandlerMethods.

/**
 * Scan beans in the ApplicationContext, detect and prepare a map of handler methods.
 */
private Collection<MappingInfo> findHandlerMethods() {
    ApplicationContext context = obtainApplicationContext();
    Map<FieldCoordinates, MappingInfo> result = new HashMap<>();
    for (String beanName : context.getBeanNamesForType(Object.class)) {
        if (beanName.startsWith(SCOPED_TARGET_NAME_PREFIX)) {
            continue;
        }
        Class<?> beanType = null;
        try {
            beanType = context.getType(beanName);
        } catch (Throwable ex) {
            // An unresolvable bean type, probably from a lazy bean - let's ignore it.
            if (logger.isTraceEnabled()) {
                logger.trace("Could not resolve type for bean '" + beanName + "'", ex);
            }
        }
        if (beanType == null || !AnnotatedElementUtils.hasAnnotation(beanType, Controller.class)) {
            continue;
        }
        Class<?> beanClass = context.getType(beanName);
        findHandlerMethods(beanName, beanClass).forEach((info) -> {
            HandlerMethod handlerMethod = info.getHandlerMethod();
            MappingInfo existing = result.put(info.getCoordinates(), info);
            if (existing != null && !existing.getHandlerMethod().equals(handlerMethod)) {
                throw new IllegalStateException("Ambiguous mapping. Cannot map '" + handlerMethod.getBean() + "' method \n" + handlerMethod + "\nto " + info.getCoordinates() + ": There is already '" + existing.getHandlerMethod().getBean() + "' bean method\n" + existing + " mapped.");
            }
        });
    }
    return result.values();
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) HashMap(java.util.HashMap) FieldCoordinates(graphql.schema.FieldCoordinates) HandlerMethod(org.springframework.graphql.data.method.HandlerMethod)

Example 4 with HandlerMethod

use of org.springframework.graphql.data.method.HandlerMethod in project spring-graphql by spring-projects.

the class AnnotatedControllerConfigurer method registerBatchLoader.

private String registerBatchLoader(MappingInfo info) {
    if (!info.isBatchMapping()) {
        throw new IllegalArgumentException("Not a @BatchMapping method: " + info);
    }
    String dataLoaderKey = info.getCoordinates().toString();
    BatchLoaderRegistry registry = obtainApplicationContext().getBean(BatchLoaderRegistry.class);
    HandlerMethod handlerMethod = info.getHandlerMethod();
    BatchLoaderHandlerMethod invocable = new BatchLoaderHandlerMethod(handlerMethod);
    Class<?> clazz = handlerMethod.getReturnType().getParameterType();
    if (clazz.equals(Flux.class) || Collection.class.isAssignableFrom(clazz)) {
        registry.forName(dataLoaderKey).registerBatchLoader(invocable::invokeForIterable);
    } else if (clazz.equals(Mono.class) || clazz.equals(Map.class)) {
        registry.forName(dataLoaderKey).registerMappedBatchLoader(invocable::invokeForMap);
    } else {
        throw new IllegalStateException("@BatchMapping method is expected to return " + "Flux<V>, List<V>, Mono<Map<K, V>>, or Map<K, V>: " + handlerMethod);
    }
    return dataLoaderKey;
}
Also used : BatchLoaderRegistry(org.springframework.graphql.execution.BatchLoaderRegistry) Flux(reactor.core.publisher.Flux) Collection(java.util.Collection) HandlerMethod(org.springframework.graphql.data.method.HandlerMethod)

Example 5 with HandlerMethod

use of org.springframework.graphql.data.method.HandlerMethod in project spring-graphql by spring-projects.

the class HandlerMethodInputValidatorTests method shouldIgnoreMethodsWithoutAnnotations.

@Test
void shouldIgnoreMethodsWithoutAnnotations() throws Exception {
    HandlerMethod method = findHandlerMethod(MyValidBean.class, "notValidatedMethod");
    assertThatNoException().isThrownBy(() -> validator.validate(method, new Object[] { "test", 12 }));
}
Also used : HandlerMethod(org.springframework.graphql.data.method.HandlerMethod) Test(org.junit.jupiter.api.Test)

Aggregations

HandlerMethod (org.springframework.graphql.data.method.HandlerMethod)7 Test (org.junit.jupiter.api.Test)3 FieldCoordinates (graphql.schema.FieldCoordinates)1 Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 ApplicationContext (org.springframework.context.ApplicationContext)1 MethodParameter (org.springframework.core.MethodParameter)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 BatchLoaderRegistry (org.springframework.graphql.execution.BatchLoaderRegistry)1 Nullable (org.springframework.lang.Nullable)1 Flux (reactor.core.publisher.Flux)1