diff --git a/spring-graphql/src/main/java/org/springframework/graphql/execution/BatchLoaderRegistry.java b/spring-graphql/src/main/java/org/springframework/graphql/execution/BatchLoaderRegistry.java
index f33265c7..7187ab46 100644
--- a/spring-graphql/src/main/java/org/springframework/graphql/execution/BatchLoaderRegistry.java
+++ b/spring-graphql/src/main/java/org/springframework/graphql/execution/BatchLoaderRegistry.java
@@ -44,16 +44,39 @@ import reactor.core.publisher.Mono;
public interface BatchLoaderRegistry {
/**
- * Start the registration of a new function for batch loading data values by
- * specifying the key and value types.
- * @param keyType the type of the key that identifies the value
- * @param valueType the type of the data value
+ * Begin the registration of a new batch load function by specifying the
+ * types of the keys and values that will be used as input and output.
+ *
+ *
When this method is used, the name for the
+ * {@link org.dataloader.DataLoader} is automatically set as defined in
+ * {@link RegistrationSpec#withName(String)}, and likewise,
+ * {@code @SchemaMapping} handler methods can transparenly locate and
+ * inject a {@code DataLoader} argument based on the generic type
+ * {@code }.
+ *
+ * @param keyType the type of keys that will be used as input
+ * @param valueType the type of value that will be returned as output
* @param the key type
* @param the value type
* @return a spec to complete the registration
*/
RegistrationSpec forTypePair(Class keyType, Class valueType);
+ /**
+ * Begin the registration of a new batch load function by specifying the
+ * name for the {@link org.dataloader.DataLoader}.
+ *
+ * Note: when this method is used, the parameter name
+ * of a {@code DataLoader} argument in a {@code @SchemaMapping} handler
+ * method needs to match the name given here.
+ *
+ * @param name the name to use to register a {@code DataLoader}
+ * @param the type of keys that will be used as input
+ * @param the type of values that will be used as output
+ * @return a spec to complete the registration
+ */
+ RegistrationSpec forName(String name);
+
/**
* Spec to complete the registration of a batch loading function.
@@ -66,7 +89,8 @@ public interface BatchLoaderRegistry {
/**
* Customize the name under which the {@link org.dataloader.DataLoader}
* is registered and can be accessed in the data layer.
- * By default, this is the full class name of the value type.
+ *
By default, this is the full class name of the value type, if the
+ * value type is specified via {@link #forTypePair(Class, Class)}.
* @param name the name to use
* @return a spec to complete the registration
*/
diff --git a/spring-graphql/src/main/java/org/springframework/graphql/execution/DefaultBatchLoaderRegistry.java b/spring-graphql/src/main/java/org/springframework/graphql/execution/DefaultBatchLoaderRegistry.java
index 05633e32..aa155446 100644
--- a/spring-graphql/src/main/java/org/springframework/graphql/execution/DefaultBatchLoaderRegistry.java
+++ b/spring-graphql/src/main/java/org/springframework/graphql/execution/DefaultBatchLoaderRegistry.java
@@ -33,6 +33,10 @@ import org.dataloader.MappedBatchLoaderWithContext;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
+import org.springframework.lang.Nullable;
+import org.springframework.util.Assert;
+import org.springframework.util.StringUtils;
+
/**
* A default implementation of {@link BatchLoaderRegistry} that accepts
* registrations, and also an implementation of {@link DataLoaderRegistrar} to
@@ -50,7 +54,12 @@ public class DefaultBatchLoaderRegistry implements BatchLoaderRegistry, DataLoad
@Override
public RegistrationSpec forTypePair(Class keyType, Class valueType) {
- return new DefaultRegistrationSpec<>(valueType.getName());
+ return new DefaultRegistrationSpec<>(valueType);
+ }
+
+ @Override
+ public RegistrationSpec forName(String name) {
+ return new DefaultRegistrationSpec<>(name);
}
@Override
@@ -75,12 +84,21 @@ public class DefaultBatchLoaderRegistry implements BatchLoaderRegistry, DataLoad
private class DefaultRegistrationSpec implements RegistrationSpec {
+ @Nullable
+ private final Class> valueType;
+
+ @Nullable
private String name;
private DataLoaderOptions options = DataLoaderOptions.newOptions();
+ public DefaultRegistrationSpec(Class valueType) {
+ this.valueType = valueType;
+ }
+
public DefaultRegistrationSpec(String name) {
this.name = name;
+ this.valueType = null;
}
@Override
@@ -104,13 +122,21 @@ public class DefaultBatchLoaderRegistry implements BatchLoaderRegistry, DataLoad
@Override
public void registerBatchLoader(BiFunction, BatchLoaderEnvironment, Flux> loader) {
DefaultBatchLoaderRegistry.this.loaders.add(
- new ReactorBatchLoader<>(this.name, loader, this.options));
+ new ReactorBatchLoader<>(initName(), loader, this.options));
}
@Override
public void registerMappedBatchLoader(BiFunction, BatchLoaderEnvironment, Mono