Search in sources :

Example 1 with BatchLoaderRegistry

use of org.springframework.graphql.execution.BatchLoaderRegistry in project spring-graphql by spring-projects.

the class BatchMappingTestSupport method createGraphQlService.

protected GraphQlService createGraphQlService(CourseController controller) {
    BatchLoaderRegistry registry = new DefaultBatchLoaderRegistry();
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.registerBean(CourseController.class, () -> controller);
    context.registerBean(BatchLoaderRegistry.class, () -> registry);
    context.refresh();
    return GraphQlSetup.schemaContent(schema).runtimeWiringForAnnotatedControllers(context).dataLoaders(registry).toGraphQlService();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) BatchLoaderRegistry(org.springframework.graphql.execution.BatchLoaderRegistry) DefaultBatchLoaderRegistry(org.springframework.graphql.execution.DefaultBatchLoaderRegistry) DefaultBatchLoaderRegistry(org.springframework.graphql.execution.DefaultBatchLoaderRegistry)

Example 2 with BatchLoaderRegistry

use of org.springframework.graphql.execution.BatchLoaderRegistry 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 3 with BatchLoaderRegistry

use of org.springframework.graphql.execution.BatchLoaderRegistry in project spring-graphql by spring-projects.

the class SchemaMappingInvocationTests method graphQlService.

private GraphQlService graphQlService() {
    BatchLoaderRegistry registry = new DefaultBatchLoaderRegistry();
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.register(BookController.class);
    context.registerBean(BatchLoaderRegistry.class, () -> registry);
    context.refresh();
    return GraphQlSetup.schemaResource(BookSource.schema).runtimeWiringForAnnotatedControllers(context).dataLoaders(registry).toGraphQlService();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) BatchLoaderRegistry(org.springframework.graphql.execution.BatchLoaderRegistry) DefaultBatchLoaderRegistry(org.springframework.graphql.execution.DefaultBatchLoaderRegistry) DefaultBatchLoaderRegistry(org.springframework.graphql.execution.DefaultBatchLoaderRegistry)

Aggregations

BatchLoaderRegistry (org.springframework.graphql.execution.BatchLoaderRegistry)3 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)2 DefaultBatchLoaderRegistry (org.springframework.graphql.execution.DefaultBatchLoaderRegistry)2 Collection (java.util.Collection)1 HandlerMethod (org.springframework.graphql.data.method.HandlerMethod)1 Flux (reactor.core.publisher.Flux)1