1
1
/*
2
- * Copyright 2002-2021 the original author or authors.
2
+ * Copyright 2002-2022 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
23
23
import java .util .concurrent .CompletionStage ;
24
24
import java .util .function .BiFunction ;
25
25
import java .util .function .Consumer ;
26
+ import java .util .function .Supplier ;
26
27
27
28
import graphql .GraphQLContext ;
28
29
import io .micrometer .context .ContextSnapshot ;
@@ -55,6 +56,25 @@ public class DefaultBatchLoaderRegistry implements BatchLoaderRegistry {
55
56
56
57
private final List <ReactorMappedBatchLoader <?,?>> mappedLoaders = new ArrayList <>();
57
58
59
+ private final Supplier <DataLoaderOptions > defaultOptionsSupplier ;
60
+
61
+
62
+ /**
63
+ * Default constructor
64
+ */
65
+ public DefaultBatchLoaderRegistry () {
66
+ this (DataLoaderOptions ::newOptions );
67
+ }
68
+
69
+ /**
70
+ * Constructor with a default {@link DataLoaderOptions} supplier to use as
71
+ * a starting point for all registrations.
72
+ * @since 1.1
73
+ */
74
+ public DefaultBatchLoaderRegistry (Supplier <DataLoaderOptions > defaultOptionsSupplier ) {
75
+ this .defaultOptionsSupplier = defaultOptionsSupplier ;
76
+ }
77
+
58
78
59
79
@ Override
60
80
public <K , V > RegistrationSpec <K , V > forTypePair (Class <K > keyType , Class <V > valueType ) {
@@ -69,14 +89,16 @@ public <K, V> RegistrationSpec<K, V> forName(String name) {
69
89
@ Override
70
90
public void registerDataLoaders (DataLoaderRegistry registry , GraphQLContext context ) {
71
91
BatchLoaderContextProvider contextProvider = () -> context ;
72
- DataLoaderOptions defaultOptions = DataLoaderOptions . newOptions (). setBatchLoaderContextProvider ( contextProvider );
92
+ DataLoaderOptions defaultOptions = this . defaultOptionsSupplier . get ( );
73
93
for (ReactorBatchLoader <?, ?> loader : this .loaders ) {
74
- DataLoaderOptions options = loader .getOptionsOrDefault (contextProvider , defaultOptions );
94
+ DataLoaderOptions options = loader .getOptions ();
95
+ options = (options != null ? options : defaultOptions ).setBatchLoaderContextProvider (contextProvider );
75
96
DataLoader <?, ?> dataLoader = DataLoaderFactory .newDataLoader (loader , options );
76
97
registerDataLoader (loader .getName (), dataLoader , registry );
77
98
}
78
99
for (ReactorMappedBatchLoader <?, ?> loader : this .mappedLoaders ) {
79
- DataLoaderOptions options = loader .getOptionsOrDefault (contextProvider , defaultOptions );
100
+ DataLoaderOptions options = loader .getOptions ();
101
+ options = (options != null ? options : defaultOptions ).setBatchLoaderContextProvider (contextProvider );
80
102
DataLoader <?, ?> dataLoader = DataLoaderFactory .newMappedDataLoader (loader , options );
81
103
registerDataLoader (loader .getName (), dataLoader , registry );
82
104
}
@@ -101,6 +123,9 @@ private class DefaultRegistrationSpec<K, V> implements RegistrationSpec<K, V> {
101
123
@ Nullable
102
124
private DataLoaderOptions options ;
103
125
126
+ @ Nullable
127
+ private Consumer <DataLoaderOptions > optionsConsumer ;
128
+
104
129
public DefaultRegistrationSpec (Class <V > valueType ) {
105
130
this .valueType = valueType ;
106
131
}
@@ -118,8 +143,8 @@ public RegistrationSpec<K, V> withName(String name) {
118
143
119
144
@ Override
120
145
public RegistrationSpec <K , V > withOptions (Consumer <DataLoaderOptions > optionsConsumer ) {
121
- this .options = (this .options != null ? this . options : DataLoaderOptions . newOptions ());
122
- optionsConsumer .accept ( this . options );
146
+ this .optionsConsumer = (this .optionsConsumer != null ?
147
+ this . optionsConsumer .andThen ( optionsConsumer ) : optionsConsumer );
123
148
return this ;
124
149
}
125
150
@@ -132,13 +157,33 @@ public RegistrationSpec<K, V> withOptions(DataLoaderOptions options) {
132
157
@ Override
133
158
public void registerBatchLoader (BiFunction <List <K >, BatchLoaderEnvironment , Flux <V >> loader ) {
134
159
DefaultBatchLoaderRegistry .this .loaders .add (
135
- new ReactorBatchLoader <>(initName (), loader , this . options ));
160
+ new ReactorBatchLoader <>(initName (), loader , initOptionsSupplier () ));
136
161
}
137
162
138
163
@ Override
139
164
public void registerMappedBatchLoader (BiFunction <Set <K >, BatchLoaderEnvironment , Mono <Map <K , V >>> loader ) {
140
165
DefaultBatchLoaderRegistry .this .mappedLoaders .add (
141
- new ReactorMappedBatchLoader <>(initName (), loader , this .options ));
166
+ new ReactorMappedBatchLoader <>(initName (), loader , initOptionsSupplier ()));
167
+ }
168
+
169
+ @ Nullable
170
+ private Supplier <DataLoaderOptions > initOptionsSupplier () {
171
+ if (this .options == null && this .optionsConsumer == null ) {
172
+ return null ;
173
+ }
174
+
175
+ Supplier <DataLoaderOptions > optionsSupplier =
176
+ (this .options != null ? () -> this .options : defaultOptionsSupplier );
177
+
178
+ if (this .optionsConsumer == null ) {
179
+ return optionsSupplier ;
180
+ }
181
+
182
+ return () -> {
183
+ DataLoaderOptions options = optionsSupplier .get ();
184
+ this .optionsConsumer .accept (options );
185
+ return options ;
186
+ };
142
187
}
143
188
144
189
private String initName () {
@@ -162,29 +207,24 @@ private static class ReactorBatchLoader<K, V> implements BatchLoaderWithContext<
162
207
private final BiFunction <List <K >, BatchLoaderEnvironment , Flux <V >> loader ;
163
208
164
209
@ Nullable
165
- private final DataLoaderOptions options ;
210
+ private final Supplier < DataLoaderOptions > optionsSupplier ;
166
211
167
212
private ReactorBatchLoader (String name ,
168
213
BiFunction <List <K >, BatchLoaderEnvironment , Flux <V >> loader ,
169
- @ Nullable DataLoaderOptions options ) {
214
+ @ Nullable Supplier < DataLoaderOptions > optionsSupplier ) {
170
215
171
216
this .name = name ;
172
217
this .loader = loader ;
173
- this .options = options ;
218
+ this .optionsSupplier = optionsSupplier ;
174
219
}
175
220
176
221
public String getName () {
177
222
return this .name ;
178
223
}
179
224
180
- public DataLoaderOptions getOptionsOrDefault (
181
- BatchLoaderContextProvider provider , DataLoaderOptions defaultOptions ) {
182
-
183
- if (this .options != null ) {
184
- return new DataLoaderOptions (this .options ).setBatchLoaderContextProvider (provider );
185
- }
186
-
187
- return defaultOptions ;
225
+ @ Nullable
226
+ public DataLoaderOptions getOptions () {
227
+ return (this .optionsSupplier != null ? this .optionsSupplier .get () : null );
188
228
}
189
229
190
230
@ Override
@@ -217,29 +257,24 @@ private static class ReactorMappedBatchLoader<K, V> implements MappedBatchLoader
217
257
private final BiFunction <Set <K >, BatchLoaderEnvironment , Mono <Map <K , V >>> loader ;
218
258
219
259
@ Nullable
220
- private final DataLoaderOptions options ;
260
+ private final Supplier < DataLoaderOptions > optionsSupplier ;
221
261
222
262
private ReactorMappedBatchLoader (String name ,
223
263
BiFunction <Set <K >, BatchLoaderEnvironment , Mono <Map <K , V >>> loader ,
224
- @ Nullable DataLoaderOptions options ) {
264
+ @ Nullable Supplier < DataLoaderOptions > optionsSupplier ) {
225
265
226
266
this .name = name ;
227
267
this .loader = loader ;
228
- this .options = options ;
268
+ this .optionsSupplier = optionsSupplier ;
229
269
}
230
270
231
271
public String getName () {
232
272
return this .name ;
233
273
}
234
274
235
- public DataLoaderOptions getOptionsOrDefault (
236
- BatchLoaderContextProvider provider , DataLoaderOptions defaultOptions ) {
237
-
238
- if (this .options != null ) {
239
- return new DataLoaderOptions (this .options ).setBatchLoaderContextProvider (provider );
240
- }
241
-
242
- return defaultOptions ;
275
+ @ Nullable
276
+ public DataLoaderOptions getOptions () {
277
+ return (this .optionsSupplier != null ? this .optionsSupplier .get () : null );
243
278
}
244
279
245
280
@ Override
0 commit comments