Skip to content

Commit

Permalink
feat(graphql): Lazy dataLoaders
Browse files Browse the repository at this point in the history
  • Loading branch information
david-leifker committed Sep 3, 2024
1 parent b2b9b89 commit 94b1c90
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.dataloader.DataLoader;
import org.dataloader.DataLoaderRegistry;

/**
* Simple wrapper around a {@link GraphQL} instance providing APIs for building an engine and
Expand Down Expand Up @@ -100,7 +99,7 @@ public ExecutionResult execute(
/*
* Init DataLoaderRegistry - should be created for each request.
*/
DataLoaderRegistry register = createDataLoaderRegistry(_dataLoaderSuppliers, context);
LazyDataLoaderRegistry register = new LazyDataLoaderRegistry(context, _dataLoaderSuppliers);

/*
* Construct execution input
Expand Down Expand Up @@ -218,14 +217,4 @@ public GraphQLEngine build() {
graphQLQueryIntrospectionEnabled);
}
}

private DataLoaderRegistry createDataLoaderRegistry(
final Map<String, Function<QueryContext, DataLoader<?, ?>>> dataLoaderSuppliers,
final QueryContext context) {
final DataLoaderRegistry registry = new DataLoaderRegistry();
for (String key : dataLoaderSuppliers.keySet()) {
registry.register(key, dataLoaderSuppliers.get(key).apply(context));
}
return registry;
}
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
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);
@Override
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);
}
}

0 comments on commit 94b1c90

Please sign in to comment.