1717package org .springframework .graphql .data .method .annotation .support ;
1818
1919import java .util .List ;
20+ import java .util .Optional ;
21+ import java .util .function .Function ;
2022import java .util .stream .Collectors ;
2123
2224import graphql .schema .DataFetchingEnvironment ;
2830import org .springframework .graphql .BookCriteria ;
2931import org .springframework .graphql .data .method .annotation .QueryMapping ;
3032import org .springframework .graphql .data .query .AbstractSortStrategy ;
31- import org .springframework .graphql .data .query .SortStrategy ;
3233
3334import static org .assertj .core .api .Assertions .assertThat ;
3435
3940 */
4041public class SortMethodArgumentResolverTests extends ArgumentResolverTestSupport {
4142
43+ private final SortMethodArgumentResolver resolver = new SortMethodArgumentResolver (new MySortStrategy ());
44+
4245 private final MethodParameter param = methodParam (BookController .class , "getBooks" , Sort .class );
4346
47+ private final MethodParameter paramOptional = methodParam (BookController .class , "getBooksOptional" , Optional .class );
48+
4449
4550 @ Test
4651 void supports () {
47- SortMethodArgumentResolver resolver = resolver ( new SimpleSortStrategy () );
48- assertThat (resolver .supportsParameter (this .param )).isTrue ();
49-
52+ assertThat ( this . resolver . supportsParameter ( this . param )). isTrue ( );
53+ assertThat (this . resolver .supportsParameter (this .paramOptional )).isTrue ();
54+
5055 MethodParameter param = methodParam (BookController .class , "getBooksByCriteria" , BookCriteria .class );
51- assertThat (resolver .supportsParameter (param )).isFalse ();
56+ assertThat (this . resolver .supportsParameter (param )).isFalse ();
5257 }
5358
59+ @ SuppressWarnings ({"unchecked" , "DataFlowIssue" , "OptionalGetWithoutIsPresent" })
5460 @ Test
5561 void resolve () throws Exception {
62+ testResolver (env -> (Sort ) this .resolver .resolveArgument (this .param , env ));
63+ testResolver (env -> ((Optional <Sort >) this .resolver .resolveArgument (this .paramOptional , env )).get ());
64+ }
65+
66+ private void testResolver (Function <DataFetchingEnvironment , Sort > resolveFunction ) throws Exception {
5667 DataFetchingEnvironment environment = environment ("""
5768 { "sortFields": ["firstName", "lastName", "id"], "sortDirection": "DESC"}"
5869 """ );
5970
60- Sort sort = ( Sort ) resolver ( new SimpleSortStrategy ()). resolveArgument ( param , environment );
71+ Sort sort = resolveFunction . apply ( environment );
6172
6273 assertThat (sort .stream ().collect (Collectors .toList ()))
6374 .hasSize (3 )
@@ -67,19 +78,20 @@ void resolve() throws Exception {
6778 new Sort .Order (Sort .Direction .DESC , "id" ));
6879 }
6980
70- private SortMethodArgumentResolver resolver (SortStrategy sortStrategy ) {
71- return new SortMethodArgumentResolver (sortStrategy );
72- }
7381
74-
75- @ SuppressWarnings ({"DataFlowIssue" , "unused" })
82+ @ SuppressWarnings ({"unused" , "DataFlowIssue" , "OptionalUsedAsFieldOrParameterType" })
7683 private static class BookController {
7784
7885 @ QueryMapping
7986 public List <Book > getBooks (Sort sort ) {
8087 return null ;
8188 }
8289
90+ @ QueryMapping
91+ public List <Book > getBooksOptional (Optional <Sort > sort ) {
92+ return null ;
93+ }
94+
8395 @ QueryMapping
8496 public List <Book > getBooksByCriteria (BookCriteria criteria ) {
8597 return null ;
@@ -88,17 +100,17 @@ public List<Book> getBooksByCriteria(BookCriteria criteria) {
88100 }
89101
90102
91- private static class SimpleSortStrategy extends AbstractSortStrategy {
103+ private static class MySortStrategy extends AbstractSortStrategy {
92104
93105 @ Override
94- protected List <String > getProperties (DataFetchingEnvironment environment ) {
95- return environment .getArgument ("sortFields" );
106+ protected List <String > getProperties (DataFetchingEnvironment env ) {
107+ return env .getArgument ("sortFields" );
96108 }
97109
98110 @ Override
99- protected Sort .Direction getDirection (DataFetchingEnvironment environment ) {
100- return ( environment . containsArgument ("sortDirection" ) ?
101- Sort .Direction .valueOf (environment . getArgument ( "sortDirection" ) ) : null );
111+ protected Sort .Direction getDirection (DataFetchingEnvironment env ) {
112+ String direction = env . getArgument ("sortDirection" );
113+ return ( direction != null ? Sort .Direction .valueOf (direction ) : null );
102114 }
103115
104116 }
0 commit comments