Skip to content

Commit cd3cf9d

Browse files
odrotbohmrstoyanchev
authored andcommitted
Adapt to API changes in Spring Data's ScrollPosition APIs.
We now use the factory methods introduced on ScrollPosition to create new instances of both Keyset- as well as OffsetScrollPosition. Also simplified a few places in which the API now provides a more convenient way to advance offsets. Related ticket: #679.
1 parent 13a95f8 commit cd3cf9d

12 files changed

+37
-37
lines changed

Diff for: spring-graphql/src/main/java/org/springframework/graphql/data/query/RepositoryUtils.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.springframework.core.ResolvableType;
2323
import org.springframework.core.annotation.AnnotatedElementUtils;
2424
import org.springframework.core.annotation.MergedAnnotations;
25-
import org.springframework.data.domain.OffsetScrollPosition;
2625
import org.springframework.data.domain.ScrollPosition;
2726
import org.springframework.data.repository.NoRepositoryBean;
2827
import org.springframework.data.repository.Repository;
@@ -39,6 +38,7 @@
3938
* Utility methods to get information for Spring Data repositories.
4039
*
4140
* @author Rossen Stoyanchev
41+
* @author Oliver Drotbohm
4242
* @since 1.0.0
4343
*/
4444
class RepositoryUtils {
@@ -85,7 +85,7 @@ public static CursorStrategy<ScrollPosition> defaultCursorStrategy() {
8585
}
8686

8787
public static ScrollSubrange defaultScrollSubrange() {
88-
return new ScrollSubrange(OffsetScrollPosition.initial(), 20, true);
88+
return new ScrollSubrange(ScrollPosition.offset(), 20, true);
8989
}
9090

9191
public static ScrollSubrange buildScrollSubrange(

Diff for: spring-graphql/src/main/java/org/springframework/graphql/data/query/ScrollPositionCursorStrategy.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* Strategy to convert a {@link ScrollPosition} to and from a String cursor.
2929
*
3030
* @author Rossen Stoyanchev
31+
* @author Oliver Drotbohm
3132
* @since 1.2.0
3233
*/
3334
public final class ScrollPositionCursorStrategy implements CursorStrategy<ScrollPosition> {
@@ -79,11 +80,11 @@ public ScrollPosition fromCursor(String cursor) {
7980
try {
8081
if (cursor.startsWith(OFFSET_PREFIX)) {
8182
long index = Long.parseLong(cursor.substring(2));
82-
return OffsetScrollPosition.of(index > 0 ? index : 0);
83+
return ScrollPosition.offset(index > 0 ? index : 0);
8384
}
8485
else if (cursor.startsWith(KEYSET_PREFIX)) {
8586
Map<String, Object> keys = this.keysetCursorStrategy.fromCursor(cursor.substring(2));
86-
return KeysetScrollPosition.of(keys);
87+
return ScrollPosition.forward(keys);
8788
}
8889
}
8990
catch (Throwable ex) {

Diff for: spring-graphql/src/main/java/org/springframework/graphql/data/query/ScrollSubrange.java

+3-7
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@
1717
package org.springframework.graphql.data.query;
1818

1919

20-
import java.util.Map;
21-
2220
import org.springframework.data.domain.KeysetScrollPosition;
23-
import org.springframework.data.domain.KeysetScrollPosition.Direction;
2421
import org.springframework.data.domain.OffsetScrollPosition;
2522
import org.springframework.data.domain.ScrollPosition;
2623
import org.springframework.graphql.data.pagination.Subrange;
@@ -36,6 +33,7 @@
3633
* always {@code true}.
3734
*
3835
* @author Rossen Stoyanchev
36+
* @author Oliver Drotbohm
3937
* @since 1.2.0
4038
*/
4139
public final class ScrollSubrange extends Subrange<ScrollPosition> {
@@ -49,12 +47,10 @@ public ScrollSubrange(@Nullable ScrollPosition pos, @Nullable Integer count, boo
4947
private static ScrollPosition initPosition(@Nullable ScrollPosition pos, @Nullable Integer count, boolean forward) {
5048
if (!forward) {
5149
if (pos instanceof OffsetScrollPosition offsetPosition && count != null) {
52-
long offset = offsetPosition.getOffset();
53-
return OffsetScrollPosition.of(offset > count ? offset - count : 0);
50+
return offsetPosition.advanceBy(-count);
5451
}
5552
else if (pos instanceof KeysetScrollPosition keysetPosition) {
56-
Map<String, Object> keys = keysetPosition.getKeys();
57-
pos = KeysetScrollPosition.of(keys, Direction.Backward);
53+
pos = keysetPosition.backward();
5854
}
5955
}
6056
return pos;

Diff for: spring-graphql/src/main/java/org/springframework/graphql/data/query/SliceConnectionAdapter.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.util.Collection;
2020

21-
import org.springframework.data.domain.OffsetScrollPosition;
2221
import org.springframework.data.domain.ScrollPosition;
2322
import org.springframework.data.domain.Slice;
2423
import org.springframework.graphql.data.pagination.ConnectionAdapter;
@@ -29,6 +28,7 @@
2928
* Adapter for {@link Slice} to {@link graphql.relay.Connection}.
3029
*
3130
* @author Rossen Stoyanchev
31+
* @author Oliver Drotbohm
3232
* @since 1.2.0
3333
*/
3434
public final class SliceConnectionAdapter
@@ -68,7 +68,7 @@ public boolean hasNext(Object container) {
6868
@Override
6969
public String cursorAt(Object container, int index) {
7070
Slice<?> slice = slice(container);
71-
ScrollPosition position = OffsetScrollPosition.of((long) slice.getNumber() * slice.getSize() + index);
71+
ScrollPosition position = ScrollPosition.offset((long) slice.getNumber() * slice.getSize() + index);
7272
return getCursorStrategy().toCursor(position);
7373
}
7474

Diff for: spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/SchemaMappingPaginationTests.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2424
import org.springframework.data.domain.OffsetScrollPosition;
25+
import org.springframework.data.domain.ScrollPosition;
2526
import org.springframework.data.domain.Window;
2627
import org.springframework.graphql.Book;
2728
import org.springframework.graphql.BookSource;
@@ -43,6 +44,7 @@
4344
* GraphQL paginated requests handled through {@code @SchemaMapping} methods.
4445
*
4546
* @author Rossen Stoyanchev
47+
* @author Oliver Drotbohm
4648
*/
4749
public class SchemaMappingPaginationTests {
4850

@@ -95,10 +97,10 @@ private static class BookController {
9597

9698
@QueryMapping
9799
public Window<Book> books(ScrollSubrange subrange) {
98-
int offset = (int) ((OffsetScrollPosition) subrange.position().orElse(OffsetScrollPosition.initial())).getOffset();
100+
int offset = (int) ((OffsetScrollPosition) subrange.position().orElse(ScrollPosition.offset())).getOffset();
99101
int count = subrange.count().orElse(5);
100102
List<Book> books = BookSource.books().subList(offset, offset + count);
101-
return Window.from(books, OffsetScrollPosition::of);
103+
return Window.from(books, ScrollPosition::offset);
102104
}
103105

104106
}

Diff for: spring-graphql/src/test/java/org/springframework/graphql/data/query/ScrollPositionCursorStrategyTests.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121

2222
import org.junit.jupiter.api.Test;
2323

24-
import org.springframework.data.domain.KeysetScrollPosition;
25-
import org.springframework.data.domain.OffsetScrollPosition;
2624
import org.springframework.data.domain.ScrollPosition;
2725

2826
import static org.assertj.core.api.Assertions.assertThat;
@@ -31,6 +29,7 @@
3129
* Unit tests for {@link ScrollPositionCursorStrategy}.
3230
*
3331
* @author Rossen Stoyanchev
32+
* @author Oliver Drotbohm
3433
*/
3534
public class ScrollPositionCursorStrategyTests {
3635

@@ -39,7 +38,7 @@ public class ScrollPositionCursorStrategyTests {
3938

4039
@Test
4140
void offsetPosition() {
42-
toAndFromCursor(OffsetScrollPosition.of(43), "O_43");
41+
toAndFromCursor(ScrollPosition.offset(43), "O_43");
4342
}
4443

4544
@Test
@@ -49,7 +48,7 @@ void keysetPosition() {
4948
keys.put("lastName", "Heller");
5049
keys.put("id", 103);
5150

52-
toAndFromCursor(KeysetScrollPosition.of(keys),
51+
toAndFromCursor(ScrollPosition.forward(keys),
5352
"K_{\"firstName\":\"Joseph\",\"lastName\":\"Heller\",\"id\":103}");
5453
}
5554

Diff for: spring-graphql/src/test/java/org/springframework/graphql/data/query/ScrollSubrangeTests.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,23 @@
2222
import org.junit.jupiter.api.Test;
2323

2424
import org.springframework.data.domain.KeysetScrollPosition;
25-
import org.springframework.data.domain.KeysetScrollPosition.Direction;
2625
import org.springframework.data.domain.OffsetScrollPosition;
2726
import org.springframework.data.domain.ScrollPosition;
27+
import org.springframework.data.domain.ScrollPosition.Direction;
2828

2929
import static org.assertj.core.api.Assertions.assertThat;
3030

3131
/**
3232
* Unit tests for {@link ScrollPositionCursorStrategy}.
3333
*
3434
* @author Rossen Stoyanchev
35+
* @author Oliver Drotbohm
3536
*/
3637
public class ScrollSubrangeTests {
3738

3839
@Test
3940
void offset() {
40-
OffsetScrollPosition position = OffsetScrollPosition.of(30);
41+
ScrollPosition position = ScrollPosition.offset(30);
4142
int count = 10;
4243

4344
ScrollSubrange subrange = new ScrollSubrange(position, count, true);
@@ -58,20 +59,20 @@ void keyset() {
5859
keys.put("lastName", "Heller");
5960
keys.put("id", 103);
6061

61-
ScrollPosition position = KeysetScrollPosition.of(keys);
62+
ScrollPosition position = ScrollPosition.forward(keys);
6263
int count = 10;
6364

6465
ScrollSubrange subrange = new ScrollSubrange(position, count, true);
6566
KeysetScrollPosition actualPosition = (KeysetScrollPosition) subrange.position().get();
6667
assertThat(actualPosition.getKeys()).isEqualTo(keys);
67-
assertThat(actualPosition.getDirection()).isEqualTo(Direction.Forward);
68+
assertThat(actualPosition.getDirection()).isEqualTo(Direction.FORWARD);
6869
assertThat(subrange.count().orElse(0)).isEqualTo(count);
6970
assertThat(subrange.forward()).isTrue();
7071

7172
subrange = new ScrollSubrange(position, count, false);
7273
actualPosition = (KeysetScrollPosition) subrange.position().get();
7374
assertThat(actualPosition.getKeys()).isEqualTo(keys);
74-
assertThat(actualPosition.getDirection()).isEqualTo(Direction.Backward);
75+
assertThat(actualPosition.getDirection()).isEqualTo(Direction.BACKWARD);
7576
assertThat(subrange.count().orElse(0)).isEqualTo(count);
7677
assertThat(subrange.forward()).isFalse();
7778
}
@@ -87,7 +88,7 @@ void nullInput() {
8788

8889
@Test
8990
void offsetBackwardPaginationNullSize() {
90-
OffsetScrollPosition position = OffsetScrollPosition.of(30);
91+
ScrollPosition position = ScrollPosition.offset(30);
9192
ScrollSubrange subrange = new ScrollSubrange(position, null, false);
9293

9394
assertThat(((OffsetScrollPosition) subrange.position().get())).isEqualTo(position);

Diff for: spring-graphql/src/test/java/org/springframework/graphql/data/query/WindowConnectionAdapterTests.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import org.junit.jupiter.api.Test;
2222

23-
import org.springframework.data.domain.OffsetScrollPosition;
23+
import org.springframework.data.domain.ScrollPosition;
2424
import org.springframework.data.domain.Window;
2525
import org.springframework.graphql.Book;
2626
import org.springframework.graphql.BookSource;
@@ -31,6 +31,7 @@
3131
* Unit tests for {@link WindowConnectionAdapter}.
3232
*
3333
* @author Rossen Stoyanchev
34+
* @author Oliver Drotbohm
3435
*/
3536
public class WindowConnectionAdapterTests {
3637

@@ -40,7 +41,7 @@ public class WindowConnectionAdapterTests {
4041
@Test
4142
void paged() {
4243
List<Book> books = BookSource.books();
43-
Window<Book> window = Window.from(books, offset -> OffsetScrollPosition.of(35 + offset), true);
44+
Window<Book> window = Window.from(books, offset -> ScrollPosition.offset(35 + offset), true);
4445

4546
assertThat(this.adapter.getContent(window)).isEqualTo(books);
4647
assertThat(this.adapter.hasNext(window)).isTrue();
@@ -51,7 +52,7 @@ void paged() {
5152
@Test
5253
void unpaged() {
5354
List<Book> books = BookSource.books();
54-
Window<Book> window = Window.from(books, OffsetScrollPosition::of);
55+
Window<Book> window = Window.from(books, ScrollPosition::offset);
5556

5657
assertThat(this.adapter.getContent(window)).isEqualTo(books);
5758
assertThat(this.adapter.hasNext(window)).isFalse();

Diff for: spring-graphql/src/test/java/org/springframework/graphql/data/query/jpa/QueryByExampleDataFetcherJpaTests.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@
3636
import org.springframework.beans.factory.annotation.Autowired;
3737
import org.springframework.context.annotation.Bean;
3838
import org.springframework.context.annotation.Configuration;
39-
import org.springframework.data.domain.OffsetScrollPosition;
40-
import org.springframework.data.domain.Sort;
39+
import org.springframework.data.domain.ScrollPosition;
4140
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
4241
import org.springframework.data.repository.query.QueryByExampleExecutor;
4342
import org.springframework.graphql.BookSource;
@@ -260,7 +259,7 @@ private static RuntimeWiringConfigurer createRuntimeWiringConfigurer(QueryByExam
260259
executor != null ? Collections.singletonList(executor) : Collections.emptyList(),
261260
Collections.emptyList(),
262261
new ScrollPositionCursorStrategy(),
263-
new ScrollSubrange(OffsetScrollPosition.initial(), 10, true));
262+
new ScrollSubrange(ScrollPosition.offset(), 10, true));
264263
}
265264

266265
private WebGraphQlRequest request(String query) {

Diff for: spring-graphql/src/test/java/org/springframework/graphql/data/query/mongo/QueryByExampleDataFetcherMongoDbTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
import org.springframework.beans.factory.annotation.Value;
3939
import org.springframework.context.annotation.Bean;
4040
import org.springframework.context.annotation.Configuration;
41-
import org.springframework.data.domain.OffsetScrollPosition;
41+
import org.springframework.data.domain.ScrollPosition;
4242
import org.springframework.data.mongodb.core.MongoTemplate;
4343
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
4444
import org.springframework.data.repository.query.QueryByExampleExecutor;
@@ -237,7 +237,7 @@ private static RuntimeWiringConfigurer createRuntimeWiringConfigurer(QueryByExam
237237
(executor != null ? Collections.singletonList(executor) : Collections.emptyList()),
238238
Collections.emptyList(),
239239
new ScrollPositionCursorStrategy(),
240-
new ScrollSubrange(OffsetScrollPosition.initial(), 10, true));
240+
new ScrollSubrange(ScrollPosition.offset(), 10, true));
241241
}
242242

243243
private WebGraphQlRequest request(String query) {

Diff for: spring-graphql/src/test/java/org/springframework/graphql/data/query/mongo/QueryByExampleDataFetcherReactiveMongoDbTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import org.springframework.beans.factory.annotation.Value;
3838
import org.springframework.context.annotation.Bean;
3939
import org.springframework.context.annotation.Configuration;
40-
import org.springframework.data.domain.OffsetScrollPosition;
40+
import org.springframework.data.domain.ScrollPosition;
4141
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
4242
import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories;
4343
import org.springframework.data.repository.query.ReactiveQueryByExampleExecutor;
@@ -210,7 +210,7 @@ private static RuntimeWiringConfigurer createRuntimeWiringConfigurer(ReactiveQue
210210
Collections.emptyList(),
211211
(executor != null ? Collections.singletonList(executor) : Collections.emptyList()),
212212
new ScrollPositionCursorStrategy(),
213-
new ScrollSubrange(OffsetScrollPosition.initial(), 10, true));
213+
new ScrollSubrange(ScrollPosition.offset(), 10, true));
214214
}
215215

216216
private WebGraphQlRequest request(String query) {

Diff for: spring-graphql/src/test/java/org/springframework/graphql/execution/SchemaMappingInspectorTests.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import reactor.core.publisher.Mono;
3535

3636
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
37-
import org.springframework.data.domain.OffsetScrollPosition;
37+
import org.springframework.data.domain.ScrollPosition;
3838
import org.springframework.data.domain.Window;
3939
import org.springframework.graphql.Author;
4040
import org.springframework.graphql.Book;
@@ -54,6 +54,7 @@
5454
*
5555
* @author Brian Clozel
5656
* @author Rossen Stoyanchev
57+
* @author Oliver Drotbohm
5758
*/
5859
class SchemaMappingInspectorTests {
5960

@@ -606,7 +607,7 @@ public List<Book> allBooks() {
606607

607608
@QueryMapping
608609
public Window<Book> paginatedBooks() {
609-
return Window.from(List.of(new Book()), OffsetScrollPosition::of);
610+
return Window.from(List.of(new Book()), ScrollPosition::offset);
610611
}
611612

612613
@SchemaMapping

0 commit comments

Comments
 (0)