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.
72
- * @since 1.1
71
+ * a starting point for batch loader registrations.
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,14 +166,19 @@ 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 = () ->
180
+ new DataLoaderOptions (this .options != null ?
181
+ this .options : DefaultBatchLoaderRegistry .this .defaultOptionsSupplier .get ());
177
182
178
183
if (this .optionsConsumer == null ) {
179
184
return optionsSupplier ;
@@ -185,14 +190,6 @@ private Supplier<DataLoaderOptions> initOptionsSupplier() {
185
190
return options ;
186
191
};
187
192
}
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
193
}
197
194
198
195
@@ -206,12 +203,11 @@ private static class ReactorBatchLoader<K, V> implements BatchLoaderWithContext<
206
203
207
204
private final BiFunction <List <K >, BatchLoaderEnvironment , Flux <V >> loader ;
208
205
209
- @ Nullable
210
206
private final Supplier <DataLoaderOptions > optionsSupplier ;
211
207
212
208
private ReactorBatchLoader (String name ,
213
209
BiFunction <List <K >, BatchLoaderEnvironment , Flux <V >> loader ,
214
- @ Nullable Supplier <DataLoaderOptions > optionsSupplier ) {
210
+ Supplier <DataLoaderOptions > optionsSupplier ) {
215
211
216
212
this .name = name ;
217
213
this .loader = loader ;
@@ -222,9 +218,8 @@ public String getName() {
222
218
return this .name ;
223
219
}
224
220
225
- @ Nullable
226
221
public DataLoaderOptions getOptions () {
227
- return ( this .optionsSupplier != null ? this . optionsSupplier . get () : null );
222
+ return this .optionsSupplier . get ();
228
223
}
229
224
230
225
@ Override
@@ -257,12 +252,11 @@ private static class ReactorMappedBatchLoader<K, V> implements MappedBatchLoader
257
252
258
253
private final BiFunction <Set <K >, BatchLoaderEnvironment , Mono <Map <K , V >>> loader ;
259
254
260
- @ Nullable
261
255
private final Supplier <DataLoaderOptions > optionsSupplier ;
262
256
263
257
private ReactorMappedBatchLoader (String name ,
264
258
BiFunction <Set <K >, BatchLoaderEnvironment , Mono <Map <K , V >>> loader ,
265
- @ Nullable Supplier <DataLoaderOptions > optionsSupplier ) {
259
+ Supplier <DataLoaderOptions > optionsSupplier ) {
266
260
267
261
this .name = name ;
268
262
this .loader = loader ;
@@ -273,9 +267,8 @@ public String getName() {
273
267
return this .name ;
274
268
}
275
269
276
- @ Nullable
277
270
public DataLoaderOptions getOptions () {
278
- return ( this .optionsSupplier != null ? this . optionsSupplier . get () : null );
271
+ return this .optionsSupplier . get ();
279
272
}
280
273
281
274
@ Override
0 commit comments