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

com.introproventures.graphql.jpa.query.schema.impl.BatchLoaderRegistry Maven / Gradle / Ivy

The newest version!
package com.introproventures.graphql.jpa.query.schema.impl;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.dataloader.DataLoader;
import org.dataloader.DataLoaderFactory;
import org.dataloader.DataLoaderOptions;
import org.dataloader.DataLoaderRegistry;
import org.dataloader.MappedBatchLoaderWithContext;

public class BatchLoaderRegistry {

    private static final Map>> mappedToManyBatchLoaders = new LinkedHashMap<>();
    private static final Map> mappedToOneBatchLoaders = new LinkedHashMap<>();
    private static BatchLoaderRegistry instance = new BatchLoaderRegistry();

    public static BatchLoaderRegistry getInstance() {
        return instance;
    }

    public static void registerToMany(
        String batchLoaderKey,
        MappedBatchLoaderWithContext> mappedBatchLoader
    ) {
        mappedToManyBatchLoaders.putIfAbsent(batchLoaderKey, mappedBatchLoader);
    }

    public static void registerToOne(
        String batchLoaderKey,
        MappedBatchLoaderWithContext mappedBatchLoader
    ) {
        mappedToOneBatchLoaders.putIfAbsent(batchLoaderKey, mappedBatchLoader);
    }

    public static Map getMappedBatchDataLoaderMap() {
        var loaders = new LinkedHashMap();

        loaders.putAll(mappedToManyBatchLoaders);
        loaders.putAll(mappedToOneBatchLoaders);

        return loaders;
    }

    public static DataLoaderRegistry newDataLoaderRegistry(DataLoaderOptions dataLoaderOptions) {
        DataLoaderRegistry dataLoaderRegistry = new DataLoaderRegistry();

        mappedToManyBatchLoaders
            .entrySet()
            .forEach(entry -> {
                DataLoader> dataLoader = DataLoaderFactory.newMappedDataLoader(
                    entry.getValue(),
                    dataLoaderOptions
                );
                dataLoaderRegistry.register(entry.getKey(), dataLoader);
            });

        mappedToOneBatchLoaders
            .entrySet()
            .forEach(entry -> {
                DataLoader dataLoader = DataLoaderFactory.newMappedDataLoader(
                    entry.getValue(),
                    dataLoaderOptions
                );
                dataLoaderRegistry.register(entry.getKey(), dataLoader);
            });

        return dataLoaderRegistry;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy