-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b2b9b89
commit 53c9ab4
Showing
2 changed files
with
23 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 22 additions & 25 deletions
47
datahub-graphql-core/src/main/java/com/linkedin/datahub/graphql/LazyDataLoaderRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,31 @@ | ||
package com.linkedin.datahub.graphql; | ||
|
||
import org.dataloader.DataLoader; | ||
import org.dataloader.DataLoaderRegistry; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
|
||
public class LazyDataLoaderManager { | ||
private final Map<String, Function<QueryContext, DataLoader<?, ?>>> dataLoaderSuppliers = new ConcurrentHashMap<>(); | ||
private final DataLoaderRegistry registry; | ||
import org.dataloader.DataLoader; | ||
import org.dataloader.DataLoaderRegistry; | ||
|
||
public LazyDataLoaderManager(Map<String, Function<QueryContext, DataLoader<?, ?>>> dataLoaderSuppliers) { | ||
this.dataLoaderSuppliers.putAll(dataLoaderSuppliers); | ||
this.registry = registry; | ||
} | ||
public class LazyDataLoaderRegistry extends DataLoaderRegistry { | ||
private final QueryContext queryContext; | ||
private final Map<String, Function<QueryContext, DataLoader<?, ?>>> dataLoaderSuppliers; | ||
private final DataLoaderRegistry registry; | ||
|
||
public <K, V> DataLoader<K, V> getDataLoader(String key) { | ||
if (!registry.getKeys().contains(key)) { | ||
Function<QueryContext, DataLoader<?, ?>> supplier = dataLoaderSuppliers.get(key); | ||
if (supplier == null) { | ||
throw new IllegalArgumentException("No DataLoader registered for key: " + key); | ||
} | ||
registry.register(key, supplier.get()); | ||
} | ||
return registry.getDataLoader(key); | ||
} | ||
public LazyDataLoaderRegistry( | ||
QueryContext queryContext, | ||
Map<String, Function<QueryContext, DataLoader<?, ?>>> dataLoaderSuppliers) { | ||
this.queryContext = queryContext; | ||
this.dataLoaderSuppliers = dataLoaderSuppliers; | ||
this.registry = new DataLoaderRegistry(); | ||
} | ||
|
||
public void clearAll() { | ||
registry.getKeys().forEach(registry::unregister); | ||
public <K, V> DataLoader<K, V> getDataLoader(String key) { | ||
if (!registry.getKeys().contains(key)) { | ||
Function<QueryContext, DataLoader<?, ?>> supplier = dataLoaderSuppliers.get(key); | ||
if (supplier == null) { | ||
throw new IllegalArgumentException("No DataLoader registered for key: " + key); | ||
} | ||
registry.register(key, supplier.apply(queryContext)); | ||
} | ||
return registry.getDataLoader(key); | ||
} | ||
} |