All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.xlrit.gears.server.graphql.EntityBatchLoader Maven / Gradle / Ivy

There is a newer version: 1.17.6
Show newest version
package com.xlrit.gears.server.graphql;

import java.util.List;
import java.util.function.BiFunction;
import jakarta.persistence.EntityManager;

import lombok.RequiredArgsConstructor;
import org.dataloader.BatchLoaderEnvironment;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.graphql.execution.BatchLoaderRegistry;
import reactor.core.publisher.Flux;

@RequiredArgsConstructor
public class EntityBatchLoader extends BaseBatchLoader {
	private static final Logger LOG = LoggerFactory.getLogger(EntityBatchLoader.class);

	private final EntityManager entityManager;
	private final Class clazz;

	@Override
	public Class getEntityClass() {
		return clazz;
	}

	@Override
	public Flux apply(List ids, BatchLoaderEnvironment env) {
		LOG.debug("Loading {} for ids {}", clazz.getSimpleName(), ids);

		List result = entityManager.unwrap(Session.class)
			.byMultipleIds(clazz)
			.enableOrderedReturn(true)
			.multiLoad(ids);

		return Flux.fromIterable(result);
	}
}

abstract class BaseBatchLoader implements BiFunction, BatchLoaderEnvironment, Flux> {
	public abstract  Class getEntityClass();

	public void registerSelf(BatchLoaderRegistry registry) {
		registry.forTypePair(String.class, getEntityClass()).registerBatchLoader(this);
	}
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy