diff --git a/spring-graphql/build.gradle b/spring-graphql/build.gradle index 8f45ef752..7e0d257cc 100644 --- a/spring-graphql/build.gradle +++ b/spring-graphql/build.gradle @@ -21,7 +21,7 @@ dependencies { compileOnly 'org.springframework.security:spring-security-core' compileOnly 'com.querydsl:querydsl-core' - compileOnly 'org.springframework.data:spring-data-commons' + compileOnly 'org.springframework.data:spring-data-commons:3.1.0-SNAPSHOT' compileOnly 'io.rsocket:rsocket-core' compileOnly 'io.rsocket:rsocket-transport-netty' @@ -44,7 +44,7 @@ dependencies { testImplementation 'org.springframework:spring-websocket' testImplementation 'org.springframework.data:spring-data-commons' testImplementation 'org.springframework.data:spring-data-keyvalue' - testImplementation 'org.springframework.data:spring-data-jpa' + testImplementation 'org.springframework.data:spring-data-jpa:3.1.0-SNAPSHOT' testImplementation 'io.micrometer:micrometer-observation-test' testImplementation 'io.micrometer:micrometer-tracing-test' testImplementation 'com.h2database:h2' diff --git a/spring-graphql/src/main/java/org/springframework/graphql/data/query/RepositoryUtils.java b/spring-graphql/src/main/java/org/springframework/graphql/data/query/RepositoryUtils.java index 747aabd23..060468113 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/data/query/RepositoryUtils.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/data/query/RepositoryUtils.java @@ -22,7 +22,6 @@ 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 @@ * 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 @@ public static CursorStrategy defaultCursorStrategy() { } public static ScrollSubrange defaultScrollSubrange() { - return new ScrollSubrange(OffsetScrollPosition.initial(), 20, true); + return new ScrollSubrange(ScrollPosition.offset(), 20, true); } public static ScrollSubrange buildScrollSubrange( diff --git a/spring-graphql/src/main/java/org/springframework/graphql/data/query/ScrollPositionCursorStrategy.java b/spring-graphql/src/main/java/org/springframework/graphql/data/query/ScrollPositionCursorStrategy.java index 25110ec9f..55c877355 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/data/query/ScrollPositionCursorStrategy.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/data/query/ScrollPositionCursorStrategy.java @@ -28,6 +28,7 @@ * 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 { @@ -79,11 +80,11 @@ public ScrollPosition fromCursor(String cursor) { 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 keys = this.keysetCursorStrategy.fromCursor(cursor.substring(2)); - return KeysetScrollPosition.of(keys); + return ScrollPosition.forward(keys); } } catch (Throwable ex) { diff --git a/spring-graphql/src/main/java/org/springframework/graphql/data/query/ScrollSubrange.java b/spring-graphql/src/main/java/org/springframework/graphql/data/query/ScrollSubrange.java index 6c1cb685a..7d89d4eeb 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/data/query/ScrollSubrange.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/data/query/ScrollSubrange.java @@ -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 @@ * always {@code true}. * * @author Rossen Stoyanchev + * @author Oliver Drotbohm * @since 1.2.0 */ public final class ScrollSubrange extends Subrange { @@ -49,12 +47,10 @@ public ScrollSubrange(@Nullable ScrollPosition pos, @Nullable Integer count, boo 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 keys = keysetPosition.getKeys(); - pos = KeysetScrollPosition.of(keys, Direction.Backward); + pos = keysetPosition.backward(); } } return pos; diff --git a/spring-graphql/src/main/java/org/springframework/graphql/data/query/SliceConnectionAdapter.java b/spring-graphql/src/main/java/org/springframework/graphql/data/query/SliceConnectionAdapter.java index b9b497b0d..07dbf2c03 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/data/query/SliceConnectionAdapter.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/data/query/SliceConnectionAdapter.java @@ -18,7 +18,6 @@ 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 @@ * 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 boolean hasNext(Object container) { @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); } diff --git a/spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/SchemaMappingPaginationTests.java b/spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/SchemaMappingPaginationTests.java index c633409d1..2dd31410f 100644 --- a/spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/SchemaMappingPaginationTests.java +++ b/spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/SchemaMappingPaginationTests.java @@ -22,6 +22,7 @@ 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 @@ * GraphQL paginated requests handled through {@code @SchemaMapping} methods. * * @author Rossen Stoyanchev + * @author Oliver Drotbohm */ public class SchemaMappingPaginationTests { @@ -95,10 +97,10 @@ private static class BookController { @QueryMapping public Window 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 books = BookSource.books().subList(offset, offset + count); - return Window.from(books, OffsetScrollPosition::of); + return Window.from(books, ScrollPosition::offset); } } diff --git a/spring-graphql/src/test/java/org/springframework/graphql/data/query/ScrollPositionCursorStrategyTests.java b/spring-graphql/src/test/java/org/springframework/graphql/data/query/ScrollPositionCursorStrategyTests.java index 450895575..5a83eee2e 100644 --- a/spring-graphql/src/test/java/org/springframework/graphql/data/query/ScrollPositionCursorStrategyTests.java +++ b/spring-graphql/src/test/java/org/springframework/graphql/data/query/ScrollPositionCursorStrategyTests.java @@ -21,8 +21,6 @@ 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 @@ * 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 @@ void keysetPosition() { keys.put("lastName", "Heller"); keys.put("id", 103); - toAndFromCursor(KeysetScrollPosition.of(keys), + toAndFromCursor(ScrollPosition.forward(keys), "K_{\"firstName\":\"Joseph\",\"lastName\":\"Heller\",\"id\":103}"); } diff --git a/spring-graphql/src/test/java/org/springframework/graphql/data/query/ScrollSubrangeTests.java b/spring-graphql/src/test/java/org/springframework/graphql/data/query/ScrollSubrangeTests.java index 2442b9ebe..81aa01fe7 100644 --- a/spring-graphql/src/test/java/org/springframework/graphql/data/query/ScrollSubrangeTests.java +++ b/spring-graphql/src/test/java/org/springframework/graphql/data/query/ScrollSubrangeTests.java @@ -22,9 +22,9 @@ 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 @@ * 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 @@ void keyset() { 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 @@ void nullInput() { @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); diff --git a/spring-graphql/src/test/java/org/springframework/graphql/data/query/WindowConnectionAdapterTests.java b/spring-graphql/src/test/java/org/springframework/graphql/data/query/WindowConnectionAdapterTests.java index 1b94c4061..8ee611cb5 100644 --- a/spring-graphql/src/test/java/org/springframework/graphql/data/query/WindowConnectionAdapterTests.java +++ b/spring-graphql/src/test/java/org/springframework/graphql/data/query/WindowConnectionAdapterTests.java @@ -20,7 +20,7 @@ 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 @@ * 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 books = BookSource.books(); - Window window = Window.from(books, offset -> OffsetScrollPosition.of(35 + offset), true); + Window 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 @@ void paged() { @Test void unpaged() { List books = BookSource.books(); - Window window = Window.from(books, OffsetScrollPosition::of); + Window window = Window.from(books, ScrollPosition::offset); assertThat(this.adapter.getContent(window)).isEqualTo(books); assertThat(this.adapter.hasNext(window)).isFalse(); diff --git a/spring-graphql/src/test/java/org/springframework/graphql/data/query/jpa/QueryByExampleDataFetcherJpaTests.java b/spring-graphql/src/test/java/org/springframework/graphql/data/query/jpa/QueryByExampleDataFetcherJpaTests.java index 7bb2dd202..b75c50a27 100644 --- a/spring-graphql/src/test/java/org/springframework/graphql/data/query/jpa/QueryByExampleDataFetcherJpaTests.java +++ b/spring-graphql/src/test/java/org/springframework/graphql/data/query/jpa/QueryByExampleDataFetcherJpaTests.java @@ -36,8 +36,7 @@ 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 @@ private static RuntimeWiringConfigurer createRuntimeWiringConfigurer(QueryByExam 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) { diff --git a/spring-graphql/src/test/java/org/springframework/graphql/data/query/mongo/QueryByExampleDataFetcherMongoDbTests.java b/spring-graphql/src/test/java/org/springframework/graphql/data/query/mongo/QueryByExampleDataFetcherMongoDbTests.java index 9aeb1382a..e3f616291 100644 --- a/spring-graphql/src/test/java/org/springframework/graphql/data/query/mongo/QueryByExampleDataFetcherMongoDbTests.java +++ b/spring-graphql/src/test/java/org/springframework/graphql/data/query/mongo/QueryByExampleDataFetcherMongoDbTests.java @@ -38,7 +38,7 @@ 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 @@ private static RuntimeWiringConfigurer createRuntimeWiringConfigurer(QueryByExam (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) { diff --git a/spring-graphql/src/test/java/org/springframework/graphql/data/query/mongo/QueryByExampleDataFetcherReactiveMongoDbTests.java b/spring-graphql/src/test/java/org/springframework/graphql/data/query/mongo/QueryByExampleDataFetcherReactiveMongoDbTests.java index 81f664d80..bce58a83e 100644 --- a/spring-graphql/src/test/java/org/springframework/graphql/data/query/mongo/QueryByExampleDataFetcherReactiveMongoDbTests.java +++ b/spring-graphql/src/test/java/org/springframework/graphql/data/query/mongo/QueryByExampleDataFetcherReactiveMongoDbTests.java @@ -37,7 +37,7 @@ 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 @@ private static RuntimeWiringConfigurer createRuntimeWiringConfigurer(ReactiveQue 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) { diff --git a/spring-graphql/src/test/java/org/springframework/graphql/execution/SchemaMappingInspectorTests.java b/spring-graphql/src/test/java/org/springframework/graphql/execution/SchemaMappingInspectorTests.java index 781d4a613..5b8b57778 100644 --- a/spring-graphql/src/test/java/org/springframework/graphql/execution/SchemaMappingInspectorTests.java +++ b/spring-graphql/src/test/java/org/springframework/graphql/execution/SchemaMappingInspectorTests.java @@ -34,7 +34,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.Author; import org.springframework.graphql.Book; @@ -54,6 +54,7 @@ * * @author Brian Clozel * @author Rossen Stoyanchev + * @author Oliver Drotbohm */ class SchemaMappingInspectorTests { @@ -606,7 +607,7 @@ public List allBooks() { @QueryMapping public Window paginatedBooks() { - return Window.from(List.of(new Book()), OffsetScrollPosition::of); + return Window.from(List.of(new Book()), ScrollPosition::offset); } @SchemaMapping