4343import org .springframework .util .StringUtils ;
4444
4545/**
46- * A default implementation of {@link BatchLoaderRegistry} that accepts
47- * registrations, and also an implementation of {@link DataLoaderRegistrar} to
48- * apply those registrations to a {@link DataLoaderRegistry}.
46+ * Default implementation of {@link BatchLoaderRegistry} that stores batch loader
47+ * registrations. Also, an implementation of {@link DataLoaderRegistrar} that
48+ * registers the batch loaders as {@link DataLoader}s in {@link DataLoaderRegistry}.
4949 *
5050 * @author Rossen Stoyanchev
5151 * @since 1.0.0
@@ -60,18 +60,19 @@ public class DefaultBatchLoaderRegistry implements BatchLoaderRegistry {
6060
6161
6262 /**
63- * Default constructor
63+ * Default constructor.
6464 */
6565 public DefaultBatchLoaderRegistry () {
6666 this (DataLoaderOptions ::newOptions );
6767 }
6868
6969 /**
7070 * Constructor with a default {@link DataLoaderOptions} supplier to use as
71- * a starting point for all registrations.
72- * @since 1.1
71+ * a starting point for batch loader registrations.
72+ * @since 1.1.0
7373 */
7474 public DefaultBatchLoaderRegistry (Supplier <DataLoaderOptions > defaultOptionsSupplier ) {
75+ Assert .notNull (defaultOptionsSupplier , "'defaultOptionsSupplier' is required" );
7576 this .defaultOptionsSupplier = defaultOptionsSupplier ;
7677 }
7778
@@ -89,16 +90,15 @@ public <K, V> RegistrationSpec<K, V> forName(String name) {
8990 @ Override
9091 public void registerDataLoaders (DataLoaderRegistry registry , GraphQLContext context ) {
9192 BatchLoaderContextProvider contextProvider = () -> context ;
92- DataLoaderOptions defaultOptions = this .defaultOptionsSupplier .get ();
9393 for (ReactorBatchLoader <?, ?> loader : this .loaders ) {
9494 DataLoaderOptions options = loader .getOptions ();
95- options = ( options != null ? options : defaultOptions ) .setBatchLoaderContextProvider (contextProvider );
95+ options = options .setBatchLoaderContextProvider (contextProvider );
9696 DataLoader <?, ?> dataLoader = DataLoaderFactory .newDataLoader (loader , options );
9797 registerDataLoader (loader .getName (), dataLoader , registry );
9898 }
9999 for (ReactorMappedBatchLoader <?, ?> loader : this .mappedLoaders ) {
100100 DataLoaderOptions options = loader .getOptions ();
101- options = ( options != null ? options : defaultOptions ) .setBatchLoaderContextProvider (contextProvider );
101+ options = options .setBatchLoaderContextProvider (contextProvider );
102102 DataLoader <?, ?> dataLoader = DataLoaderFactory .newMappedDataLoader (loader , options );
103103 registerDataLoader (loader .getName (), dataLoader , registry );
104104 }
@@ -166,14 +166,19 @@ public void registerMappedBatchLoader(BiFunction<Set<K>, BatchLoaderEnvironment,
166166 new ReactorMappedBatchLoader <>(initName (), loader , initOptionsSupplier ()));
167167 }
168168
169- @ Nullable
170- private Supplier <DataLoaderOptions > initOptionsSupplier () {
171- if (this .options == null && this .optionsConsumer == null ) {
172- return null ;
169+ private String initName () {
170+ if (StringUtils .hasText (this .name )) {
171+ return this .name ;
173172 }
173+ Assert .notNull (this .valueType , "Value type not available to select a default DataLoader name." );
174+ return (StringUtils .hasText (this .name ) ? this .name : this .valueType .getName ());
175+ }
174176
175- Supplier <DataLoaderOptions > optionsSupplier =
176- (this .options != null ? () -> this .options : defaultOptionsSupplier );
177+ private Supplier <DataLoaderOptions > initOptionsSupplier () {
178+
179+ Supplier <DataLoaderOptions > optionsSupplier = () ->
180+ new DataLoaderOptions (this .options != null ?
181+ this .options : DefaultBatchLoaderRegistry .this .defaultOptionsSupplier .get ());
177182
178183 if (this .optionsConsumer == null ) {
179184 return optionsSupplier ;
@@ -185,14 +190,6 @@ private Supplier<DataLoaderOptions> initOptionsSupplier() {
185190 return options ;
186191 };
187192 }
188-
189- private String initName () {
190- if (StringUtils .hasText (this .name )) {
191- return this .name ;
192- }
193- Assert .notNull (this .valueType , "Value type not available to select a default DataLoader name." );
194- return (StringUtils .hasText (this .name ) ? this .name : this .valueType .getName ());
195- }
196193 }
197194
198195
@@ -206,12 +203,11 @@ private static class ReactorBatchLoader<K, V> implements BatchLoaderWithContext<
206203
207204 private final BiFunction <List <K >, BatchLoaderEnvironment , Flux <V >> loader ;
208205
209- @ Nullable
210206 private final Supplier <DataLoaderOptions > optionsSupplier ;
211207
212208 private ReactorBatchLoader (String name ,
213209 BiFunction <List <K >, BatchLoaderEnvironment , Flux <V >> loader ,
214- @ Nullable Supplier <DataLoaderOptions > optionsSupplier ) {
210+ Supplier <DataLoaderOptions > optionsSupplier ) {
215211
216212 this .name = name ;
217213 this .loader = loader ;
@@ -222,9 +218,8 @@ public String getName() {
222218 return this .name ;
223219 }
224220
225- @ Nullable
226221 public DataLoaderOptions getOptions () {
227- return ( this .optionsSupplier != null ? this . optionsSupplier . get () : null );
222+ return this .optionsSupplier . get ();
228223 }
229224
230225 @ Override
@@ -257,12 +252,11 @@ private static class ReactorMappedBatchLoader<K, V> implements MappedBatchLoader
257252
258253 private final BiFunction <Set <K >, BatchLoaderEnvironment , Mono <Map <K , V >>> loader ;
259254
260- @ Nullable
261255 private final Supplier <DataLoaderOptions > optionsSupplier ;
262256
263257 private ReactorMappedBatchLoader (String name ,
264258 BiFunction <Set <K >, BatchLoaderEnvironment , Mono <Map <K , V >>> loader ,
265- @ Nullable Supplier <DataLoaderOptions > optionsSupplier ) {
259+ Supplier <DataLoaderOptions > optionsSupplier ) {
266260
267261 this .name = name ;
268262 this .loader = loader ;
@@ -273,9 +267,8 @@ public String getName() {
273267 return this .name ;
274268 }
275269
276- @ Nullable
277270 public DataLoaderOptions getOptions () {
278- return ( this .optionsSupplier != null ? this . optionsSupplier . get () : null );
271+ return this .optionsSupplier . get ();
279272 }
280273
281274 @ Override
0 commit comments