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.
This commit is contained in:
Oliver Drotbohm
2023-04-27 19:05:26 +02:00
committed by rstoyanchev
parent 13a95f8e89
commit cd3cf9d47c
12 changed files with 37 additions and 37 deletions

View File

@@ -22,7 +22,6 @@ import graphql.schema.DataFetchingEnvironment;
import org.springframework.core.ResolvableType;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.data.domain.OffsetScrollPosition;
import org.springframework.data.domain.ScrollPosition;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.Repository;
@@ -39,6 +38,7 @@ import org.springframework.util.StringUtils;
* Utility methods to get information for Spring Data repositories.
*
* @author Rossen Stoyanchev
* @author Oliver Drotbohm
* @since 1.0.0
*/
class RepositoryUtils {
@@ -85,7 +85,7 @@ class RepositoryUtils {
}
public static ScrollSubrange defaultScrollSubrange() {
return new ScrollSubrange(OffsetScrollPosition.initial(), 20, true);
return new ScrollSubrange(ScrollPosition.offset(), 20, true);
}
public static ScrollSubrange buildScrollSubrange(

View File

@@ -28,6 +28,7 @@ import org.springframework.util.Assert;
* Strategy to convert a {@link ScrollPosition} to and from a String cursor.
*
* @author Rossen Stoyanchev
* @author Oliver Drotbohm
* @since 1.2.0
*/
public final class ScrollPositionCursorStrategy implements CursorStrategy<ScrollPosition> {
@@ -79,11 +80,11 @@ public final class ScrollPositionCursorStrategy implements CursorStrategy<Scroll
try {
if (cursor.startsWith(OFFSET_PREFIX)) {
long index = Long.parseLong(cursor.substring(2));
return OffsetScrollPosition.of(index > 0 ? index : 0);
return ScrollPosition.offset(index > 0 ? index : 0);
}
else if (cursor.startsWith(KEYSET_PREFIX)) {
Map<String, Object> keys = this.keysetCursorStrategy.fromCursor(cursor.substring(2));
return KeysetScrollPosition.of(keys);
return ScrollPosition.forward(keys);
}
}
catch (Throwable ex) {

View File

@@ -17,10 +17,7 @@
package org.springframework.graphql.data.query;
import java.util.Map;
import org.springframework.data.domain.KeysetScrollPosition;
import org.springframework.data.domain.KeysetScrollPosition.Direction;
import org.springframework.data.domain.OffsetScrollPosition;
import org.springframework.data.domain.ScrollPosition;
import org.springframework.graphql.data.pagination.Subrange;
@@ -36,6 +33,7 @@ import org.springframework.lang.Nullable;
* always {@code true}.
*
* @author Rossen Stoyanchev
* @author Oliver Drotbohm
* @since 1.2.0
*/
public final class ScrollSubrange extends Subrange<ScrollPosition> {
@@ -49,12 +47,10 @@ public final class ScrollSubrange extends Subrange<ScrollPosition> {
private static ScrollPosition initPosition(@Nullable ScrollPosition pos, @Nullable Integer count, boolean forward) {
if (!forward) {
if (pos instanceof OffsetScrollPosition offsetPosition && count != null) {
long offset = offsetPosition.getOffset();
return OffsetScrollPosition.of(offset > count ? offset - count : 0);
return offsetPosition.advanceBy(-count);
}
else if (pos instanceof KeysetScrollPosition keysetPosition) {
Map<String, Object> keys = keysetPosition.getKeys();
pos = KeysetScrollPosition.of(keys, Direction.Backward);
pos = keysetPosition.backward();
}
}
return pos;

View File

@@ -18,7 +18,6 @@ package org.springframework.graphql.data.query;
import java.util.Collection;
import org.springframework.data.domain.OffsetScrollPosition;
import org.springframework.data.domain.ScrollPosition;
import org.springframework.data.domain.Slice;
import org.springframework.graphql.data.pagination.ConnectionAdapter;
@@ -29,6 +28,7 @@ import org.springframework.graphql.data.pagination.CursorStrategy;
* Adapter for {@link Slice} to {@link graphql.relay.Connection}.
*
* @author Rossen Stoyanchev
* @author Oliver Drotbohm
* @since 1.2.0
*/
public final class SliceConnectionAdapter
@@ -68,7 +68,7 @@ public final class SliceConnectionAdapter
@Override
public String cursorAt(Object container, int index) {
Slice<?> slice = slice(container);
ScrollPosition position = OffsetScrollPosition.of((long) slice.getNumber() * slice.getSize() + index);
ScrollPosition position = ScrollPosition.offset((long) slice.getNumber() * slice.getSize() + index);
return getCursorStrategy().toCursor(position);
}

View File

@@ -22,6 +22,7 @@ import reactor.core.publisher.Mono;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.data.domain.OffsetScrollPosition;
import org.springframework.data.domain.ScrollPosition;
import org.springframework.data.domain.Window;
import org.springframework.graphql.Book;
import org.springframework.graphql.BookSource;
@@ -43,6 +44,7 @@ import org.springframework.stereotype.Controller;
* GraphQL paginated requests handled through {@code @SchemaMapping} methods.
*
* @author Rossen Stoyanchev
* @author Oliver Drotbohm
*/
public class SchemaMappingPaginationTests {
@@ -95,10 +97,10 @@ public class SchemaMappingPaginationTests {
@QueryMapping
public Window<Book> books(ScrollSubrange subrange) {
int offset = (int) ((OffsetScrollPosition) subrange.position().orElse(OffsetScrollPosition.initial())).getOffset();
int offset = (int) ((OffsetScrollPosition) subrange.position().orElse(ScrollPosition.offset())).getOffset();
int count = subrange.count().orElse(5);
List<Book> books = BookSource.books().subList(offset, offset + count);
return Window.from(books, OffsetScrollPosition::of);
return Window.from(books, ScrollPosition::offset);
}
}

View File

@@ -21,8 +21,6 @@ import java.util.Map;
import org.junit.jupiter.api.Test;
import org.springframework.data.domain.KeysetScrollPosition;
import org.springframework.data.domain.OffsetScrollPosition;
import org.springframework.data.domain.ScrollPosition;
import static org.assertj.core.api.Assertions.assertThat;
@@ -31,6 +29,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* Unit tests for {@link ScrollPositionCursorStrategy}.
*
* @author Rossen Stoyanchev
* @author Oliver Drotbohm
*/
public class ScrollPositionCursorStrategyTests {
@@ -39,7 +38,7 @@ public class ScrollPositionCursorStrategyTests {
@Test
void offsetPosition() {
toAndFromCursor(OffsetScrollPosition.of(43), "O_43");
toAndFromCursor(ScrollPosition.offset(43), "O_43");
}
@Test
@@ -49,7 +48,7 @@ public class ScrollPositionCursorStrategyTests {
keys.put("lastName", "Heller");
keys.put("id", 103);
toAndFromCursor(KeysetScrollPosition.of(keys),
toAndFromCursor(ScrollPosition.forward(keys),
"K_{\"firstName\":\"Joseph\",\"lastName\":\"Heller\",\"id\":103}");
}

View File

@@ -22,9 +22,9 @@ import java.util.Map;
import org.junit.jupiter.api.Test;
import org.springframework.data.domain.KeysetScrollPosition;
import org.springframework.data.domain.KeysetScrollPosition.Direction;
import org.springframework.data.domain.OffsetScrollPosition;
import org.springframework.data.domain.ScrollPosition;
import org.springframework.data.domain.ScrollPosition.Direction;
import static org.assertj.core.api.Assertions.assertThat;
@@ -32,12 +32,13 @@ import static org.assertj.core.api.Assertions.assertThat;
* Unit tests for {@link ScrollPositionCursorStrategy}.
*
* @author Rossen Stoyanchev
* @author Oliver Drotbohm
*/
public class ScrollSubrangeTests {
@Test
void offset() {
OffsetScrollPosition position = OffsetScrollPosition.of(30);
ScrollPosition position = ScrollPosition.offset(30);
int count = 10;
ScrollSubrange subrange = new ScrollSubrange(position, count, true);
@@ -58,20 +59,20 @@ public class ScrollSubrangeTests {
keys.put("lastName", "Heller");
keys.put("id", 103);
ScrollPosition position = KeysetScrollPosition.of(keys);
ScrollPosition position = ScrollPosition.forward(keys);
int count = 10;
ScrollSubrange subrange = new ScrollSubrange(position, count, true);
KeysetScrollPosition actualPosition = (KeysetScrollPosition) subrange.position().get();
assertThat(actualPosition.getKeys()).isEqualTo(keys);
assertThat(actualPosition.getDirection()).isEqualTo(Direction.Forward);
assertThat(actualPosition.getDirection()).isEqualTo(Direction.FORWARD);
assertThat(subrange.count().orElse(0)).isEqualTo(count);
assertThat(subrange.forward()).isTrue();
subrange = new ScrollSubrange(position, count, false);
actualPosition = (KeysetScrollPosition) subrange.position().get();
assertThat(actualPosition.getKeys()).isEqualTo(keys);
assertThat(actualPosition.getDirection()).isEqualTo(Direction.Backward);
assertThat(actualPosition.getDirection()).isEqualTo(Direction.BACKWARD);
assertThat(subrange.count().orElse(0)).isEqualTo(count);
assertThat(subrange.forward()).isFalse();
}
@@ -87,7 +88,7 @@ public class ScrollSubrangeTests {
@Test
void offsetBackwardPaginationNullSize() {
OffsetScrollPosition position = OffsetScrollPosition.of(30);
ScrollPosition position = ScrollPosition.offset(30);
ScrollSubrange subrange = new ScrollSubrange(position, null, false);
assertThat(((OffsetScrollPosition) subrange.position().get())).isEqualTo(position);

View File

@@ -20,7 +20,7 @@ import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.data.domain.OffsetScrollPosition;
import org.springframework.data.domain.ScrollPosition;
import org.springframework.data.domain.Window;
import org.springframework.graphql.Book;
import org.springframework.graphql.BookSource;
@@ -31,6 +31,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* Unit tests for {@link WindowConnectionAdapter}.
*
* @author Rossen Stoyanchev
* @author Oliver Drotbohm
*/
public class WindowConnectionAdapterTests {
@@ -40,7 +41,7 @@ public class WindowConnectionAdapterTests {
@Test
void paged() {
List<Book> books = BookSource.books();
Window<Book> window = Window.from(books, offset -> OffsetScrollPosition.of(35 + offset), true);
Window<Book> window = Window.from(books, offset -> ScrollPosition.offset(35 + offset), true);
assertThat(this.adapter.getContent(window)).isEqualTo(books);
assertThat(this.adapter.hasNext(window)).isTrue();
@@ -51,7 +52,7 @@ public class WindowConnectionAdapterTests {
@Test
void unpaged() {
List<Book> books = BookSource.books();
Window<Book> window = Window.from(books, OffsetScrollPosition::of);
Window<Book> window = Window.from(books, ScrollPosition::offset);
assertThat(this.adapter.getContent(window)).isEqualTo(books);
assertThat(this.adapter.hasNext(window)).isFalse();

View File

@@ -36,8 +36,7 @@ import reactor.core.publisher.Mono;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.OffsetScrollPosition;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.ScrollPosition;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.repository.query.QueryByExampleExecutor;
import org.springframework.graphql.BookSource;
@@ -260,7 +259,7 @@ class QueryByExampleDataFetcherJpaTests {
executor != null ? Collections.singletonList(executor) : Collections.emptyList(),
Collections.emptyList(),
new ScrollPositionCursorStrategy(),
new ScrollSubrange(OffsetScrollPosition.initial(), 10, true));
new ScrollSubrange(ScrollPosition.offset(), 10, true));
}
private WebGraphQlRequest request(String query) {

View File

@@ -38,7 +38,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.OffsetScrollPosition;
import org.springframework.data.domain.ScrollPosition;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import org.springframework.data.repository.query.QueryByExampleExecutor;
@@ -237,7 +237,7 @@ class QueryByExampleDataFetcherMongoDbTests {
(executor != null ? Collections.singletonList(executor) : Collections.emptyList()),
Collections.emptyList(),
new ScrollPositionCursorStrategy(),
new ScrollSubrange(OffsetScrollPosition.initial(), 10, true));
new ScrollSubrange(ScrollPosition.offset(), 10, true));
}
private WebGraphQlRequest request(String query) {

View File

@@ -37,7 +37,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.OffsetScrollPosition;
import org.springframework.data.domain.ScrollPosition;
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories;
import org.springframework.data.repository.query.ReactiveQueryByExampleExecutor;
@@ -210,7 +210,7 @@ class QueryByExampleDataFetcherReactiveMongoDbTests {
Collections.emptyList(),
(executor != null ? Collections.singletonList(executor) : Collections.emptyList()),
new ScrollPositionCursorStrategy(),
new ScrollSubrange(OffsetScrollPosition.initial(), 10, true));
new ScrollSubrange(ScrollPosition.offset(), 10, true));
}
private WebGraphQlRequest request(String query) {

View File

@@ -34,7 +34,7 @@ import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.data.domain.OffsetScrollPosition;
import org.springframework.data.domain.ScrollPosition;
import org.springframework.data.domain.Window;
import org.springframework.graphql.Author;
import org.springframework.graphql.Book;
@@ -54,6 +54,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Brian Clozel
* @author Rossen Stoyanchev
* @author Oliver Drotbohm
*/
class SchemaMappingInspectorTests {
@@ -606,7 +607,7 @@ class SchemaMappingInspectorTests {
@QueryMapping
public Window<Book> paginatedBooks() {
return Window.from(List.of(new Book()), OffsetScrollPosition::of);
return Window.from(List.of(new Book()), ScrollPosition::offset);
}
@SchemaMapping