Skip to content

Commit

Permalink
Add dataloader for micronode list values
Browse files Browse the repository at this point in the history
  • Loading branch information
npomaroli committed Jan 16, 2024
1 parent c89265d commit d871b84
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,13 @@ default Stream<Pair<HibNodeFieldContainer, Collection<? extends HibNodeFieldCont
*/
Map<String, List<String>> getStringListFieldValues(List<String> listUuids);

/**
* Get the Micronode list field values for the given list UUIDs
* @param listUuids list UUIDs
* @return map of list UUIDs to lists of micronode field values
*/
Map<String, List<HibMicronode>> getMicronodeListFieldValues(List<String> listUuids);

/**
* Load the micronodes for the given collection of micronode fields
* @param micronodeFields micronode fields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,11 @@ public Map<String, List<String>> getStringListFieldValues(List<String> listUuids
throw new NotImplementedException("Prefetching of list values is not implemented");
}

@Override
public Map<String, List<HibMicronode>> getMicronodeListFieldValues(List<String> listUuids) {
throw new NotImplementedException("Prefetching of list values is not implemented");
}

@Override
public Map<HibMicronodeField, HibMicronode> getMicronodes(Collection<HibMicronodeField> micronodeFields) {
return micronodeFields.stream().distinct().collect(Collectors.toMap(Function.identity(), HibMicronodeField::getMicronode));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public void handleQuery(GraphQLContext gc, String body) {
dataLoaderRegistry.register(FieldDefinitionProvider.NUMBER_LIST_VALUES_DATA_LOADER_KEY, DataLoader.newDataLoader(typeProvider.getFieldDefProvider().NUMBER_LIST_VALUE_LOADER, options));
dataLoaderRegistry.register(FieldDefinitionProvider.HTML_LIST_VALUES_DATA_LOADER_KEY, DataLoader.newDataLoader(typeProvider.getFieldDefProvider().HTML_LIST_VALUE_LOADER, options));
dataLoaderRegistry.register(FieldDefinitionProvider.STRING_LIST_VALUES_DATA_LOADER_KEY, DataLoader.newDataLoader(typeProvider.getFieldDefProvider().STRING_LIST_VALUE_LOADER, options));
dataLoaderRegistry.register(FieldDefinitionProvider.MICRONODE_LIST_VALUES_DATA_LOADER_KEY, DataLoader.newDataLoader(typeProvider.getFieldDefProvider().MICRONODE_LIST_VALUE_LOADER, options));
dataLoaderRegistry.register(FieldDefinitionProvider.MICRONODE_DATA_LOADER_KEY, DataLoader.newDataLoader(typeProvider.getFieldDefProvider().MICRONODE_LOADER, options));

ExecutionInput executionInput = ExecutionInput
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
import static graphql.schema.GraphQLObjectType.newObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -125,6 +124,11 @@ public class FieldDefinitionProvider extends AbstractTypeProvider {
*/
public static final String STRING_LIST_VALUES_DATA_LOADER_KEY = "stringListLoader";

/**
* Key for the data loader for micronode list field values
*/
public static final String MICRONODE_LIST_VALUES_DATA_LOADER_KEY = "micronodeUuidListLoader";

/**
* Key for the data loader for micronode field values
*/
Expand Down Expand Up @@ -217,6 +221,14 @@ private static <U, V> CompletionStage<List<List<V>>> listValueDataLoader(List<St
return listValueDataLoader(keys, contentDao::getStringListFieldValues, Functions.identity());
};

/**
* DataLoader implementation for values of micronode lists
*/
public BatchLoaderWithContext<String, List<HibMicronode>> MICRONODE_LIST_VALUE_LOADER = (keys, environment) -> {
ContentDao contentDao = Tx.get().contentDao();
return listValueDataLoader(keys, contentDao::getMicronodeListFieldValues, Functions.identity());
};

/**
* DataLoader implementation for micronodes
*/
Expand Down Expand Up @@ -686,7 +698,14 @@ public Optional<GraphQLFieldDefinition> createListDef(GraphQLContext context, Li
if (micronodeList == null) {
return null;
}
return micronodeList.getList().stream().map(item -> item.getMicronode()).collect(Collectors.toList());

String micronodeListUuid = micronodeList.getUuid();
if (contentDao.supportsPrefetchingListFieldValues() && !StringUtils.isEmpty(micronodeListUuid)) {
DataLoader<String, List<HibMicronode>> micronodeListValueLoader = env.getDataLoader(FieldDefinitionProvider.MICRONODE_LIST_VALUES_DATA_LOADER_KEY);
return micronodeListValueLoader.load(micronodeListUuid);
} else {
return micronodeList.getList().stream().map(item -> item.getMicronode()).collect(Collectors.toList());
}
default:
return null;
}
Expand Down

0 comments on commit d871b84

Please sign in to comment.