graphql-java / java-dataloader

A Java 8 port of Facebook DataLoader

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Make the DataLoader constructor protected to make it more customisable

drupalspring opened this issue · comments

I would like to make the DataLoader to load the Key context in a type safe manner and in order to reuse the current infrastructure , I choose to extend the DataLoader :

public abstract class MyDataLoader<K, V, C> extends DataLoader<K, V> {

      public MyDataLoader(){
          super(newMappedBatchLoaderWithContext() , null); 
       }

       public CompletableFuture<V> loadWithKeyContext(K key, C keyContext) {
         return super.load(key, keyContext);
    }

      public abstract MappedBatchLoaderWithContext<K, V> newMappedBatchLoaderWithContext();
}

But the constructor DataLoader(Object batchLoadFunction, DataLoaderOptions options) in DataLoader are in the private scope now. Is it a good idea to make it be the protected scoped to provide more options for developers in case they want to customise their DataLoader?

Rather than extend the class I think you should use composition and wrap it. Inheritance creates fragile SPI code and is very hard for the implementer to have the freedom to improve the code.

So MyDataLoader is not a DataLoader - rather MyDataLoader contains a DataLoader and delegates to it

But because there is no interface for the DataLoader now , if I create a new type called MyDataLoader that wraps the DataLoader internally, basically I cannot use and adapt to the existing dataloader related codes mainly because DataLoader is a concrete type without any abstraction now.

For example, DataLoaderRegistry internally only allow to store DataLoader type. DataFetchingEnvironment#getDataLoader() only return DataLoader type etc.

So maybe a better idea is to extract an interface from the DataLoader such that developers can implement their own DataLoader type ?

I think an interface might be a better idea as you say. IDataLoader perhaps

I am loathe to expose the DataLoader constructors directly. If you look now it has a helper that is the real implementation and I was only able to do that because I am free to rejig its implementation.