|
| 1 | +/* |
| 2 | + * Copyright 2002-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.graphql.data.method.annotation.support; |
| 17 | + |
| 18 | +import java.util.List; |
| 19 | +import java.util.function.BiConsumer; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper; |
| 23 | + |
| 24 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 25 | +import org.springframework.data.domain.OffsetScrollPosition; |
| 26 | +import org.springframework.data.domain.Window; |
| 27 | +import org.springframework.graphql.Book; |
| 28 | +import org.springframework.graphql.BookSource; |
| 29 | +import org.springframework.graphql.ExecutionGraphQlResponse; |
| 30 | +import org.springframework.graphql.ExecutionGraphQlService; |
| 31 | +import org.springframework.graphql.GraphQlSetup; |
| 32 | +import org.springframework.graphql.TestExecutionRequest; |
| 33 | +import org.springframework.graphql.data.method.annotation.QueryMapping; |
| 34 | +import org.springframework.graphql.data.pagination.ConnectionFieldTypeVisitor; |
| 35 | +import org.springframework.graphql.data.query.ScrollPositionCursorStrategy; |
| 36 | +import org.springframework.graphql.data.query.ScrollRequest; |
| 37 | +import org.springframework.graphql.data.query.WindowConnectionAdapter; |
| 38 | +import org.springframework.graphql.execution.ConnectionTypeGenerator; |
| 39 | +import org.springframework.stereotype.Controller; |
| 40 | + |
| 41 | +import static org.assertj.core.api.Assertions.assertThat; |
| 42 | + |
| 43 | +/** |
| 44 | + * GraphQL paginated requests handled through {@code @SchemaMapping} methods. |
| 45 | + * |
| 46 | + * @author Rossen Stoyanchev |
| 47 | + */ |
| 48 | +public class SchemaMappingPaginationTests { |
| 49 | + |
| 50 | + private static final String SCHEMA = """ |
| 51 | + type Query { |
| 52 | + books(first:Int, after:String): BookConnection |
| 53 | + } |
| 54 | + type Book { |
| 55 | + id: ID |
| 56 | + name: String |
| 57 | + } |
| 58 | + """; |
| 59 | + |
| 60 | + |
| 61 | + @Test |
| 62 | + void forwardPagination() throws Exception { |
| 63 | + |
| 64 | + String document = """ |
| 65 | + { |
| 66 | + books(first:2, after:"O_3") { |
| 67 | + edges { |
| 68 | + cursor, |
| 69 | + node { |
| 70 | + id |
| 71 | + name |
| 72 | + } |
| 73 | + } |
| 74 | + pageInfo { |
| 75 | + startCursor, |
| 76 | + endCursor, |
| 77 | + hasPreviousPage, |
| 78 | + hasNextPage |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + """; |
| 83 | + |
| 84 | + ExecutionGraphQlService graphQlService = graphQlService((configurer, setup) -> { |
| 85 | + |
| 86 | + ConnectionTypeGenerator typeGenerator = new ConnectionTypeGenerator(); |
| 87 | + setup.typeDefinitionRegistryConfigurer(typeGenerator::generateConnectionTypes); |
| 88 | + |
| 89 | + ScrollPositionCursorStrategy cursorStrategy = new ScrollPositionCursorStrategy(); |
| 90 | + WindowConnectionAdapter connectionAdapter = new WindowConnectionAdapter(cursorStrategy); |
| 91 | + setup.typeVisitor(ConnectionFieldTypeVisitor.create(List.of(connectionAdapter))); |
| 92 | + |
| 93 | + configurer.setCursorStrategy(cursorStrategy); |
| 94 | + }); |
| 95 | + |
| 96 | + ExecutionGraphQlResponse response = |
| 97 | + graphQlService.execute(TestExecutionRequest.forDocument(document)).block(); |
| 98 | + |
| 99 | + assertThat(new ObjectMapper().writeValueAsString(response.getData())) |
| 100 | + .as("Errors: " + response.getErrors()).isEqualTo( |
| 101 | + "{\"books\":{" + |
| 102 | + "\"edges\":[" + |
| 103 | + "{\"cursor\":\"O_0\",\"node\":{\"id\":\"4\",\"name\":\"To The Lighthouse\"}}," + |
| 104 | + "{\"cursor\":\"O_1\",\"node\":{\"id\":\"5\",\"name\":\"Animal Farm\"}}" + |
| 105 | + "]," + |
| 106 | + "\"pageInfo\":{" + |
| 107 | + "\"startCursor\":\"O_0\"," + |
| 108 | + "\"endCursor\":\"O_1\"," + |
| 109 | + "\"hasPreviousPage\":false," + |
| 110 | + "\"hasNextPage\":false" + |
| 111 | + "}}}"); |
| 112 | + } |
| 113 | + |
| 114 | + private ExecutionGraphQlService graphQlService(BiConsumer<AnnotatedControllerConfigurer, GraphQlSetup> consumer) { |
| 115 | + |
| 116 | + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); |
| 117 | + context.register(BookController.class); |
| 118 | + context.refresh(); |
| 119 | + |
| 120 | + AnnotatedControllerConfigurer configurer = new AnnotatedControllerConfigurer(); |
| 121 | + configurer.setApplicationContext(context); |
| 122 | + |
| 123 | + GraphQlSetup setup = GraphQlSetup.schemaContent(this.SCHEMA).runtimeWiring(configurer); |
| 124 | + consumer.accept(configurer, setup); |
| 125 | + |
| 126 | + configurer.afterPropertiesSet(); |
| 127 | + |
| 128 | + return setup.toGraphQlService(); |
| 129 | + } |
| 130 | + |
| 131 | + |
| 132 | + @SuppressWarnings("unused") |
| 133 | + @Controller |
| 134 | + private static class BookController { |
| 135 | + |
| 136 | + @QueryMapping |
| 137 | + public Window<Book> books(ScrollRequest request) { |
| 138 | + int offset = (int) ((OffsetScrollPosition) request.position().get()).getOffset(); |
| 139 | + int count = request.count().get(); |
| 140 | + List<Book> books = BookSource.books().subList(offset, offset + count); |
| 141 | + return Window.from(books, OffsetScrollPosition::of); |
| 142 | + } |
| 143 | + |
| 144 | + } |
| 145 | + |
| 146 | +} |
0 commit comments