Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt to API changes in Spring Data Commons' ScrollPosition API #682

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions spring-graphql/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -85,7 +85,7 @@ public static CursorStrategy<ScrollPosition> defaultCursorStrategy() {
}

public static ScrollSubrange defaultScrollSubrange() {
return new ScrollSubrange(OffsetScrollPosition.initial(), 20, true);
return new ScrollSubrange(ScrollPosition.offset(), 20, true);
}

public static ScrollSubrange buildScrollSubrange(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ScrollPosition> {
Expand Down Expand Up @@ -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<String, Object> keys = this.keysetCursorStrategy.fromCursor(cursor.substring(2));
return KeysetScrollPosition.of(keys);
return ScrollPosition.forward(keys);
}
}
catch (Throwable ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,6 +33,7 @@
* always {@code true}.
*
* @author Rossen Stoyanchev
* @author Oliver Drotbohm
* @since 1.2.0
*/
public final class ScrollSubrange extends Subrange<ScrollPosition> {
Expand All @@ -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<String, Object> keys = keysetPosition.getKeys();
pos = KeysetScrollPosition.of(keys, Direction.Backward);
pos = keysetPosition.backward();
}
}
return pos;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -43,6 +44,7 @@
* GraphQL paginated requests handled through {@code @SchemaMapping} methods.
*
* @author Rossen Stoyanchev
* @author Oliver Drotbohm
*/
public class SchemaMappingPaginationTests {

Expand Down Expand Up @@ -95,10 +97,10 @@ private static class BookController {

@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);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,6 +29,7 @@
* Unit tests for {@link ScrollPositionCursorStrategy}.
*
* @author Rossen Stoyanchev
* @author Oliver Drotbohm
*/
public class ScrollPositionCursorStrategyTests {

Expand All @@ -39,7 +38,7 @@ public class ScrollPositionCursorStrategyTests {

@Test
void offsetPosition() {
toAndFromCursor(OffsetScrollPosition.of(43), "O_43");
toAndFromCursor(ScrollPosition.offset(43), "O_43");
}

@Test
Expand All @@ -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}");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,23 @@
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;

/**
* 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);
Expand All @@ -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();
}
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,6 +31,7 @@
* Unit tests for {@link WindowConnectionAdapter}.
*
* @author Rossen Stoyanchev
* @author Oliver Drotbohm
*/
public class WindowConnectionAdapterTests {

Expand All @@ -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();
Expand All @@ -51,7 +52,7 @@ void paged() {
@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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -54,6 +54,7 @@
*
* @author Brian Clozel
* @author Rossen Stoyanchev
* @author Oliver Drotbohm
*/
class SchemaMappingInspectorTests {

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

@QueryMapping
public Window<Book> paginatedBooks() {
return Window.from(List.of(new Book()), OffsetScrollPosition::of);
return Window.from(List.of(new Book()), ScrollPosition::offset);
}

@SchemaMapping
Expand Down