-
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.
feat(graphql): Lazy dataLoaders (#11293)
- Loading branch information
1 parent
e34a628
commit 2946131
Showing
5 changed files
with
74 additions
and
18 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
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
53 changes: 53 additions & 0 deletions
53
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.linkedin.datahub.graphql; | ||
|
||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.dataloader.DataLoader; | ||
import org.dataloader.DataLoaderRegistry; | ||
|
||
/** | ||
* The purpose of this class is to avoid loading 42+ dataLoaders when many of the graphql queries do | ||
* not use all of them. | ||
*/ | ||
@Slf4j | ||
public class LazyDataLoaderRegistry extends DataLoaderRegistry { | ||
private final QueryContext queryContext; | ||
private final Map<String, Function<QueryContext, DataLoader<?, ?>>> dataLoaderSuppliers; | ||
|
||
public LazyDataLoaderRegistry( | ||
QueryContext queryContext, | ||
Map<String, Function<QueryContext, DataLoader<?, ?>>> dataLoaderSuppliers) { | ||
super(); | ||
this.queryContext = queryContext; | ||
this.dataLoaderSuppliers = new ConcurrentHashMap<>(dataLoaderSuppliers); | ||
} | ||
|
||
@Override | ||
public <K, V> DataLoader<K, V> getDataLoader(String key) { | ||
return super.computeIfAbsent( | ||
key, | ||
k -> { | ||
Function<QueryContext, DataLoader<?, ?>> supplier = dataLoaderSuppliers.get(key); | ||
if (supplier == null) { | ||
throw new IllegalArgumentException("No DataLoader registered for key: " + key); | ||
} | ||
return supplier.apply(queryContext); | ||
}); | ||
} | ||
|
||
@Override | ||
public Set<String> getKeys() { | ||
return Stream.concat(dataLoaders.keySet().stream(), dataLoaderSuppliers.keySet().stream()) | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
@Override | ||
public DataLoaderRegistry combine(DataLoaderRegistry registry) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} |
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
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