Pagination test refactoring
This commit is contained in:
@@ -22,15 +22,19 @@ import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import jakarta.annotation.Nullable;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
public class BookSource {
|
||||
|
||||
public static final Resource schema = new ClassPathResource("books/schema.graphqls");
|
||||
|
||||
public static final Resource paginationSchema = new ClassPathResource("books/pagination-schema.graphqls");
|
||||
|
||||
|
||||
private static final Map<Long, Book> booksMap = new HashMap<>();
|
||||
|
||||
@@ -88,4 +92,25 @@ public class BookSource {
|
||||
return authorsMap.get(id);
|
||||
}
|
||||
|
||||
public static String booksConnectionQuery(@Nullable String arguments) {
|
||||
arguments = StringUtils.hasText(arguments) ? "(" + arguments + ")" : "";
|
||||
return "{" +
|
||||
" books" + arguments + " {" +
|
||||
" edges {" +
|
||||
" cursor," +
|
||||
" node {" +
|
||||
" id" +
|
||||
" name" +
|
||||
" }" +
|
||||
" }" +
|
||||
" pageInfo {" +
|
||||
" startCursor," +
|
||||
" endCursor," +
|
||||
" hasPreviousPage," +
|
||||
" hasNextPage" +
|
||||
" }" +
|
||||
" }" +
|
||||
"}";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -131,6 +131,11 @@ public class ResponseHelper {
|
||||
return new Error(index);
|
||||
}
|
||||
|
||||
public void assertData(String expectedJson) {
|
||||
expectedJson = "{\"data\":" + expectedJson + "}";
|
||||
assertThat(this.documentContext.jsonString()).as("Errors: " + this.errors).isEqualTo(expectedJson);
|
||||
}
|
||||
|
||||
|
||||
public static ResponseHelper forResult(ExecutionResult result) {
|
||||
return new ResponseHelper(result.toSpecification(), result.getErrors());
|
||||
|
||||
@@ -16,10 +16,9 @@
|
||||
package org.springframework.graphql.data.method.annotation.support;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.data.domain.OffsetScrollPosition;
|
||||
@@ -29,6 +28,7 @@ import org.springframework.graphql.BookSource;
|
||||
import org.springframework.graphql.ExecutionGraphQlResponse;
|
||||
import org.springframework.graphql.ExecutionGraphQlService;
|
||||
import org.springframework.graphql.GraphQlSetup;
|
||||
import org.springframework.graphql.ResponseHelper;
|
||||
import org.springframework.graphql.TestExecutionRequest;
|
||||
import org.springframework.graphql.data.method.annotation.QueryMapping;
|
||||
import org.springframework.graphql.data.pagination.ConnectionFieldTypeVisitor;
|
||||
@@ -39,8 +39,6 @@ import org.springframework.graphql.data.query.WindowConnectionAdapter;
|
||||
import org.springframework.graphql.execution.ConnectionTypeDefinitionConfigurer;
|
||||
import org.springframework.stereotype.Controller;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* GraphQL paginated requests handled through {@code @SchemaMapping} methods.
|
||||
*
|
||||
@@ -48,47 +46,15 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
*/
|
||||
public class SchemaMappingPaginationTests {
|
||||
|
||||
private static final String SCHEMA = """
|
||||
type Query {
|
||||
books(first:Int, after:String): BookConnection
|
||||
}
|
||||
type Book {
|
||||
id: ID
|
||||
name: String
|
||||
}
|
||||
""";
|
||||
|
||||
|
||||
@Test
|
||||
void forwardPagination() throws Exception {
|
||||
void forwardPagination() {
|
||||
|
||||
String document = """
|
||||
{
|
||||
books(first:2, after:"O_3") {
|
||||
edges {
|
||||
cursor,
|
||||
node {
|
||||
id
|
||||
name
|
||||
}
|
||||
}
|
||||
pageInfo {
|
||||
startCursor,
|
||||
endCursor,
|
||||
hasPreviousPage,
|
||||
hasNextPage
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
String query = BookSource.booksConnectionQuery("first:2, after:\"O_3\"");
|
||||
|
||||
ExecutionGraphQlService graphQlService = graphQlService();
|
||||
Mono<ExecutionGraphQlResponse> response = graphQlService().execute(TestExecutionRequest.forDocument(query));
|
||||
|
||||
ExecutionGraphQlResponse response =
|
||||
graphQlService.execute(TestExecutionRequest.forDocument(document)).block();
|
||||
|
||||
assertThat(new ObjectMapper().writeValueAsString(response.getData()))
|
||||
.as("Errors: " + response.getErrors()).isEqualTo(
|
||||
ResponseHelper.forResponse(response).assertData(
|
||||
"{\"books\":{" +
|
||||
"\"edges\":[" +
|
||||
"{\"cursor\":\"O_0\",\"node\":{\"id\":\"4\",\"name\":\"To The Lighthouse\"}}," +
|
||||
@@ -115,7 +81,7 @@ public class SchemaMappingPaginationTests {
|
||||
configurer.setApplicationContext(context);
|
||||
configurer.afterPropertiesSet();
|
||||
|
||||
GraphQlSetup setup = GraphQlSetup.schemaContent(SCHEMA).runtimeWiring(configurer);
|
||||
GraphQlSetup setup = GraphQlSetup.schemaResource(BookSource.paginationSchema).runtimeWiring(configurer);
|
||||
setup.typeDefinitionConfigurer(new ConnectionTypeDefinitionConfigurer());
|
||||
setup.typeVisitor(ConnectionFieldTypeVisitor.create(List.of(new WindowConnectionAdapter(cursorStrategy))));
|
||||
|
||||
|
||||
@@ -20,16 +20,15 @@ import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.graphql.BookSource;
|
||||
import org.springframework.graphql.ExecutionGraphQlResponse;
|
||||
import org.springframework.graphql.GraphQlSetup;
|
||||
import org.springframework.graphql.ResponseHelper;
|
||||
import org.springframework.graphql.TestExecutionRequest;
|
||||
import org.springframework.graphql.execution.ConnectionTypeDefinitionConfigurer;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ConnectionFieldTypeVisitor}.
|
||||
*
|
||||
@@ -39,49 +38,22 @@ public class ConnectionFieldTypeVisitorTests {
|
||||
|
||||
|
||||
@Test
|
||||
void dataFetcherDecoration() throws Exception {
|
||||
void dataFetcherDecoration() {
|
||||
|
||||
String schemaContent = """
|
||||
type Query {
|
||||
books: BookConnection
|
||||
}
|
||||
type Book {
|
||||
id: ID
|
||||
name: String
|
||||
}
|
||||
""";
|
||||
|
||||
String document = "{ " +
|
||||
" books { " +
|
||||
" edges {" +
|
||||
" cursor," +
|
||||
" node {" +
|
||||
" id" +
|
||||
" name" +
|
||||
" }" +
|
||||
" }" +
|
||||
" pageInfo {" +
|
||||
" startCursor," +
|
||||
" endCursor," +
|
||||
" hasPreviousPage," +
|
||||
" hasNextPage" +
|
||||
" }" +
|
||||
" }" +
|
||||
"}";
|
||||
String document = BookSource.booksConnectionQuery("");
|
||||
|
||||
TestConnectionAdapter adapter = new TestConnectionAdapter();
|
||||
adapter.setInitialOffset(30);
|
||||
adapter.setHasNext(true);
|
||||
|
||||
ExecutionGraphQlResponse response = GraphQlSetup.schemaContent(schemaContent)
|
||||
Mono<ExecutionGraphQlResponse> response = GraphQlSetup.schemaResource(BookSource.paginationSchema)
|
||||
.dataFetcher("Query", "books", env -> BookSource.books())
|
||||
.typeDefinitionConfigurer(new ConnectionTypeDefinitionConfigurer())
|
||||
.typeVisitor(ConnectionFieldTypeVisitor.create(List.of(adapter)))
|
||||
.toGraphQlService()
|
||||
.execute(TestExecutionRequest.forDocument(document))
|
||||
.block();
|
||||
.execute(TestExecutionRequest.forDocument(document));
|
||||
|
||||
assertThat(new ObjectMapper().writeValueAsString(response.getData())).isEqualTo(
|
||||
ResponseHelper.forResponse(response).assertData(
|
||||
"{\"books\":{" +
|
||||
"\"edges\":[" +
|
||||
"{\"cursor\":\"T_30\",\"node\":{\"id\":\"1\",\"name\":\"Nineteen Eighty-Four\"}}," +
|
||||
|
||||
@@ -27,16 +27,15 @@ import graphql.relay.DefaultPageInfo;
|
||||
import graphql.relay.Edge;
|
||||
import graphql.schema.DataFetcher;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.graphql.Book;
|
||||
import org.springframework.graphql.BookSource;
|
||||
import org.springframework.graphql.ExecutionGraphQlResponse;
|
||||
import org.springframework.graphql.GraphQlSetup;
|
||||
import org.springframework.graphql.ResponseHelper;
|
||||
import org.springframework.graphql.TestExecutionRequest;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ConnectionTypeDefinitionConfigurer}.
|
||||
*
|
||||
@@ -46,48 +45,21 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
public class ConnectionTypeDefinitionConfigurerTests {
|
||||
|
||||
@Test
|
||||
void connectionTypeGeneration() throws Exception {
|
||||
|
||||
String schema = """
|
||||
type Query {
|
||||
books: BookConnection
|
||||
}
|
||||
type Book {
|
||||
id: ID
|
||||
name: String
|
||||
}
|
||||
""";
|
||||
void connectionTypeGeneration() {
|
||||
|
||||
List<Book> books = BookSource.books();
|
||||
|
||||
DataFetcher<?> dataFetcher = environment ->
|
||||
createConnection(books, book -> new DefaultConnectionCursor("book:" + book.getId()));
|
||||
|
||||
String document = "{ " +
|
||||
" books { " +
|
||||
" edges {" +
|
||||
" cursor," +
|
||||
" node {" +
|
||||
" id" +
|
||||
" name" +
|
||||
" }" +
|
||||
" }" +
|
||||
" pageInfo {" +
|
||||
" startCursor," +
|
||||
" endCursor," +
|
||||
" hasPreviousPage," +
|
||||
" hasNextPage" +
|
||||
" }" +
|
||||
" }" +
|
||||
"}";
|
||||
String document = BookSource.booksConnectionQuery("");
|
||||
|
||||
ExecutionGraphQlResponse response = initGraphQlSetup(schema)
|
||||
Mono<ExecutionGraphQlResponse> response = initGraphQlSetup()
|
||||
.dataFetcher("Query", "books", dataFetcher)
|
||||
.toGraphQlService()
|
||||
.execute(TestExecutionRequest.forDocument(document))
|
||||
.block();
|
||||
.execute(TestExecutionRequest.forDocument(document));
|
||||
|
||||
assertThat(new ObjectMapper().writeValueAsString(response.getData())).isEqualTo(
|
||||
ResponseHelper.forResponse(response).assertData(
|
||||
"{\"books\":{" +
|
||||
"\"edges\":[" +
|
||||
"{\"cursor\":\"book:1\",\"node\":{\"id\":\"1\",\"name\":\"Nineteen Eighty-Four\"}}," +
|
||||
@@ -107,8 +79,9 @@ public class ConnectionTypeDefinitionConfigurerTests {
|
||||
);
|
||||
}
|
||||
|
||||
private GraphQlSetup initGraphQlSetup(String schema) {
|
||||
return GraphQlSetup.schemaContent(schema).typeDefinitionConfigurer(new ConnectionTypeDefinitionConfigurer());
|
||||
private GraphQlSetup initGraphQlSetup() {
|
||||
return GraphQlSetup.schemaResource(BookSource.paginationSchema)
|
||||
.typeDefinitionConfigurer(new ConnectionTypeDefinitionConfigurer());
|
||||
}
|
||||
|
||||
private static <N> Connection<N> createConnection(
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
type Query {
|
||||
books(first:Int, after:String): BookConnection
|
||||
}
|
||||
|
||||
type Book {
|
||||
id: ID
|
||||
name: String
|
||||
}
|
||||
Reference in New Issue
Block a user