58
58
import org .springframework .core .ResolvableType ;
59
59
import org .springframework .core .annotation .AnnotatedElementUtils ;
60
60
import org .springframework .core .convert .ConversionService ;
61
+ import org .springframework .data .domain .ScrollPosition ;
61
62
import org .springframework .format .FormatterRegistrar ;
62
63
import org .springframework .format .support .DefaultFormattingConversionService ;
63
64
import org .springframework .format .support .FormattingConversionService ;
82
83
import org .springframework .validation .DataBinder ;
83
84
84
85
/**
85
- * {@link RuntimeWiringConfigurer} that detects {@link SchemaMapping @SchemaMapping}
86
- * annotated handler methods in {@link Controller @Controller} classes and
87
- * registers them as {@link DataFetcher}s.
86
+ * {@link RuntimeWiringConfigurer} that finds {@link SchemaMapping @SchemaMapping}
87
+ * annotated handler methods in {@link Controller @Controller} classes declared in
88
+ * Spring configuration, and registers them as {@link DataFetcher}s.
88
89
*
89
90
* <p>In addition to initializing a {@link RuntimeWiring.Builder}, this class, also
90
91
* provides an option to {@link #configure(GraphQLCodeRegistry.Builder) configure}
91
92
* data fetchers on a {@link GraphQLCodeRegistry.Builder}.
92
93
*
94
+ * <p>This class detects the following strategies in Spring configuration,
95
+ * expecting to find a single, unique bean of that type:
96
+ * <ul>
97
+ * <li>{@link CursorStrategy} -- if Spring Data is present, and the strategy
98
+ * supports {@code ScrollPosition}, then {@link ScrollSubrangeMethodArgumentResolver}
99
+ * is configured for use. If not, then {@link SubrangeMethodArgumentResolver}
100
+ * is added instead.
101
+ * <li>{@link SortStrategy} -- if present, then {@link SortMethodArgumentResolver}
102
+ * is configured for use.
103
+ * </ul>
104
+ *
105
+ *
106
+ *
93
107
* @author Rossen Stoyanchev
94
108
* @author Brian Clozel
95
109
* @since 1.0.0
@@ -125,9 +139,6 @@ public class AnnotatedControllerConfigurer implements ApplicationContextAware, I
125
139
126
140
private final FormattingConversionService conversionService = new DefaultFormattingConversionService ();
127
141
128
- @ Nullable
129
- private CursorStrategy <?> cursorStrategy ;
130
-
131
142
private final List <HandlerMethodArgumentResolver > customArgumentResolvers = new ArrayList <>(8 );
132
143
133
144
@ Nullable
@@ -156,21 +167,6 @@ public void addFormatterRegistrar(FormatterRegistrar registrar) {
156
167
registrar .registerFormatters (this .conversionService );
157
168
}
158
169
159
- /**
160
- * Configure a {@link CursorStrategy} to handle pagination requests, which
161
- * results in one of the following:
162
- * <ul>
163
- * <li>If Spring Data is present, and the strategy supports {@code ScrollPosition},
164
- * then {@link ScrollSubrangeMethodArgumentResolver} is configured as a method
165
- * argument resolver.
166
- * <li>Otherwise {@link SubrangeMethodArgumentResolver} is added.
167
- * </ul>
168
- * @since 1.2
169
- */
170
- public void setCursorStrategy (@ Nullable CursorStrategy <?> cursorStrategy ) {
171
- this .cursorStrategy = cursorStrategy ;
172
- }
173
-
174
170
/**
175
171
* Add a {@link HandlerMethodArgumentResolver} for custom controller method
176
172
* arguments. Such custom resolvers are ordered after built-in resolvers
@@ -270,18 +266,8 @@ private HandlerMethodArgumentResolverComposite initArgumentResolvers() {
270
266
// Type based
271
267
resolvers .addResolver (new DataFetchingEnvironmentMethodArgumentResolver ());
272
268
resolvers .addResolver (new DataLoaderMethodArgumentResolver ());
273
- if (this .cursorStrategy != null ) {
274
- resolvers .addResolver (createSubrangeMethodArgumentResolver (this .cursorStrategy ));
275
- }
276
- if (springDataPresent ) {
277
- try {
278
- resolvers .addResolver (
279
- new SortMethodArgumentResolver (obtainApplicationContext ().getBean (SortStrategy .class )));
280
- }
281
- catch (NoSuchBeanDefinitionException ex ) {
282
- // ignore
283
- }
284
- }
269
+ addSubrangeMethodArgumentResolver (resolvers );
270
+ addSortMethodArgumentResolver (resolvers );
285
271
if (springSecurityPresent ) {
286
272
ApplicationContext context = obtainApplicationContext ();
287
273
resolvers .addResolver (new PrincipalMethodArgumentResolver ());
@@ -299,15 +285,34 @@ private HandlerMethodArgumentResolverComposite initArgumentResolvers() {
299
285
return resolvers ;
300
286
}
301
287
302
- @ SuppressWarnings ("unchecked" )
303
- private static HandlerMethodArgumentResolver createSubrangeMethodArgumentResolver (CursorStrategy <?> strategy ) {
288
+ @ SuppressWarnings ({"unchecked" , "CastCanBeRemovedNarrowingVariableType" })
289
+ private void addSubrangeMethodArgumentResolver (HandlerMethodArgumentResolverComposite resolvers ) {
290
+ try {
291
+ CursorStrategy <?> strategy = obtainApplicationContext ().getBean (CursorStrategy .class );
292
+ if (springDataPresent ) {
293
+ if (strategy .supports (ScrollPosition .class )) {
294
+ CursorStrategy <ScrollPosition > strategyToUse = (CursorStrategy <ScrollPosition >) strategy ;
295
+ resolvers .addResolver (new ScrollSubrangeMethodArgumentResolver (strategyToUse ));
296
+ return ;
297
+ }
298
+ }
299
+ resolvers .addResolver (new SubrangeMethodArgumentResolver <>(strategy ));
300
+ }
301
+ catch (NoSuchBeanDefinitionException ex ) {
302
+ // ignore
303
+ }
304
+ }
305
+
306
+ private void addSortMethodArgumentResolver (HandlerMethodArgumentResolverComposite resolvers ) {
304
307
if (springDataPresent ) {
305
- if (strategy .supports (org .springframework .data .domain .ScrollPosition .class )) {
306
- return new ScrollSubrangeMethodArgumentResolver (
307
- (CursorStrategy <org .springframework .data .domain .ScrollPosition >) strategy );
308
+ try {
309
+ SortStrategy strategy = obtainApplicationContext ().getBean (SortStrategy .class );
310
+ resolvers .addResolver (new SortMethodArgumentResolver (strategy ));
311
+ }
312
+ catch (NoSuchBeanDefinitionException ex ) {
313
+ // ignore
308
314
}
309
315
}
310
- return new SubrangeMethodArgumentResolver <>(strategy );
311
316
}
312
317
313
318
protected final ApplicationContext obtainApplicationContext () {
0 commit comments