graphql-java / java-dataloader

A Java 8 port of Facebook DataLoader

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Allow default value in DataLoaderOptions when a value is missing in a mapped batch loader

bbakerman opened this issue · comments

Taken from graphql-java/graphql-java#1403

MappedBatchLoader is a great addition to the data loading architecture. Sadly, we can't use it in many cases. Frequently we data load lists like so:

type Parent {
  id: String!
  children: [Child!]!  # lazy-loaded via data loader, can not be null
}

When data loading children the return type of the mapped batch loading function is Map<String, List>. When a key is not present in the map, DataLoaderHelper::invokeMapBatchLoader substitutes null. However, in this case outlined above it should instead substitute an empty list.

I propose adding an additional data loader option that would let us set this default value to an arbitrary value. Another solution that might suffice would be the helper first testing whether the loadResult is a Collection and if so, substituting an empty collection by default.

I think the default substitution of null is fine for missing keys. If you want to return an empty list or other default values you should do that should be in your own load method. For the example above the mapped batch loading function Map<String, List> should itself take responsiblilty to return an emptyList for the given String in the map it returns.

Closing as timed out.