43
43
import org .springframework .util .StringUtils ;
44
44
45
45
/**
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}.
49
49
*
50
50
* @author Rossen Stoyanchev
51
51
* @since 1.0.0
@@ -60,18 +60,19 @@ public class DefaultBatchLoaderRegistry implements BatchLoaderRegistry {
60
60
61
61
62
62
/**
63
- * Default constructor
63
+ * Default constructor.
64
64
*/
65
65
public DefaultBatchLoaderRegistry () {
66
66
this (DataLoaderOptions ::newOptions );
67
67
}
68
68
69
69
/**
70
70
* Constructor with a default {@link DataLoaderOptions} supplier to use as
71
- * a starting point for all registrations.
71
+ * a starting point for batch loader registrations.
72
72
* @since 1.1.0
73
73
*/
74
74
public DefaultBatchLoaderRegistry (Supplier <DataLoaderOptions > defaultOptionsSupplier ) {
75
+ Assert .notNull (defaultOptionsSupplier , "'defaultOptionsSupplier' is required" );
75
76
this .defaultOptionsSupplier = defaultOptionsSupplier ;
76
77
}
77
78
@@ -89,16 +90,15 @@ public <K, V> RegistrationSpec<K, V> forName(String name) {
89
90
@ Override
90
91
public void registerDataLoaders (DataLoaderRegistry registry , GraphQLContext context ) {
91
92
BatchLoaderContextProvider contextProvider = () -> context ;
92
- DataLoaderOptions defaultOptions = this .defaultOptionsSupplier .get ();
93
93
for (ReactorBatchLoader <?, ?> loader : this .loaders ) {
94
94
DataLoaderOptions options = loader .getOptions ();
95
- options = ( options != null ? options : defaultOptions ) .setBatchLoaderContextProvider (contextProvider );
95
+ options = options .setBatchLoaderContextProvider (contextProvider );
96
96
DataLoader <?, ?> dataLoader = DataLoaderFactory .newDataLoader (loader , options );
97
97
registerDataLoader (loader .getName (), dataLoader , registry );
98
98
}
99
99
for (ReactorMappedBatchLoader <?, ?> loader : this .mappedLoaders ) {
100
100
DataLoaderOptions options = loader .getOptions ();
101
- options = ( options != null ? options : defaultOptions ) .setBatchLoaderContextProvider (contextProvider );
101
+ options = options .setBatchLoaderContextProvider (contextProvider );
102
102
DataLoader <?, ?> dataLoader = DataLoaderFactory .newMappedDataLoader (loader , options );
103
103
registerDataLoader (loader .getName (), dataLoader , registry );
104
104
}
@@ -166,33 +166,29 @@ public void registerMappedBatchLoader(BiFunction<Set<K>, BatchLoaderEnvironment,
166
166
new ReactorMappedBatchLoader <>(initName (), loader , initOptionsSupplier ()));
167
167
}
168
168
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 ;
173
172
}
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
+ }
174
176
175
- Supplier <DataLoaderOptions > optionsSupplier =
176
- (this .options != null ? () -> this .options : defaultOptionsSupplier );
177
+ private Supplier <DataLoaderOptions > initOptionsSupplier () {
178
+
179
+ Supplier <DataLoaderOptions > optionsSupplier = (this .options != null ?
180
+ () -> this .options : DefaultBatchLoaderRegistry .this .defaultOptionsSupplier );
177
181
178
182
if (this .optionsConsumer == null ) {
179
183
return optionsSupplier ;
180
184
}
181
185
182
186
return () -> {
183
- DataLoaderOptions options = optionsSupplier .get ();
187
+ DataLoaderOptions options = new DataLoaderOptions ( optionsSupplier .get () );
184
188
this .optionsConsumer .accept (options );
185
189
return options ;
186
190
};
187
191
}
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
- }
196
192
}
197
193
198
194
@@ -206,12 +202,11 @@ private static class ReactorBatchLoader<K, V> implements BatchLoaderWithContext<
206
202
207
203
private final BiFunction <List <K >, BatchLoaderEnvironment , Flux <V >> loader ;
208
204
209
- @ Nullable
210
205
private final Supplier <DataLoaderOptions > optionsSupplier ;
211
206
212
207
private ReactorBatchLoader (String name ,
213
208
BiFunction <List <K >, BatchLoaderEnvironment , Flux <V >> loader ,
214
- @ Nullable Supplier <DataLoaderOptions > optionsSupplier ) {
209
+ Supplier <DataLoaderOptions > optionsSupplier ) {
215
210
216
211
this .name = name ;
217
212
this .loader = loader ;
@@ -222,9 +217,8 @@ public String getName() {
222
217
return this .name ;
223
218
}
224
219
225
- @ Nullable
226
220
public DataLoaderOptions getOptions () {
227
- return ( this .optionsSupplier != null ? this . optionsSupplier . get () : null );
221
+ return this .optionsSupplier . get ();
228
222
}
229
223
230
224
@ Override
@@ -257,12 +251,11 @@ private static class ReactorMappedBatchLoader<K, V> implements MappedBatchLoader
257
251
258
252
private final BiFunction <Set <K >, BatchLoaderEnvironment , Mono <Map <K , V >>> loader ;
259
253
260
- @ Nullable
261
254
private final Supplier <DataLoaderOptions > optionsSupplier ;
262
255
263
256
private ReactorMappedBatchLoader (String name ,
264
257
BiFunction <Set <K >, BatchLoaderEnvironment , Mono <Map <K , V >>> loader ,
265
- @ Nullable Supplier <DataLoaderOptions > optionsSupplier ) {
258
+ Supplier <DataLoaderOptions > optionsSupplier ) {
266
259
267
260
this .name = name ;
268
261
this .loader = loader ;
@@ -273,9 +266,8 @@ public String getName() {
273
266
return this .name ;
274
267
}
275
268
276
- @ Nullable
277
269
public DataLoaderOptions getOptions () {
278
- return ( this .optionsSupplier != null ? this . optionsSupplier . get () : null );
270
+ return this .optionsSupplier . get ();
279
271
}
280
272
281
273
@ Override
0 commit comments