Skip to content

Commit da00c8b

Browse files
committed
Support Optional<Sort> method argument
See gh-620
1 parent 9c53c59 commit da00c8b

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

spring-graphql/src/main/java/org/springframework/graphql/data/method/annotation/support/SortMethodArgumentResolver.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.springframework.graphql.data.method.annotation.support;
1818

1919

20+
import java.util.Optional;
21+
2022
import graphql.schema.DataFetchingEnvironment;
2123

2224
import org.springframework.core.MethodParameter;
@@ -45,12 +47,15 @@ public SortMethodArgumentResolver(SortStrategy sortStrategy) {
4547

4648
@Override
4749
public boolean supportsParameter(MethodParameter parameter) {
48-
return parameter.getParameterType().equals(Sort.class);
50+
return parameter.nestedIfOptional().getNestedParameterType().equals(Sort.class);
4951
}
5052

5153
@Override
5254
public Object resolveArgument(MethodParameter parameter, DataFetchingEnvironment environment) {
5355
Sort sort = this.sortStrategy.extract(environment);
56+
if (parameter.isOptional()) {
57+
return Optional.ofNullable(sort);
58+
}
5459
return (sort != null ? sort : Sort.unsorted());
5560
}
5661

spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/SortMethodArgumentResolverTests.java

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.springframework.graphql.data.method.annotation.support;
1818

1919
import java.util.List;
20+
import java.util.Optional;
21+
import java.util.function.Function;
2022
import java.util.stream.Collectors;
2123

2224
import graphql.schema.DataFetchingEnvironment;
@@ -28,7 +30,6 @@
2830
import org.springframework.graphql.BookCriteria;
2931
import org.springframework.graphql.data.method.annotation.QueryMapping;
3032
import org.springframework.graphql.data.query.AbstractSortStrategy;
31-
import org.springframework.graphql.data.query.SortStrategy;
3233

3334
import static org.assertj.core.api.Assertions.assertThat;
3435

@@ -39,25 +40,35 @@
3940
*/
4041
public 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

Comments
 (0)