|
19 | 19 | import java.net.URI;
|
20 | 20 | import java.util.Collections;
|
21 | 21 | import java.util.List;
|
| 22 | +import java.util.Map; |
22 | 23 | import java.util.function.Consumer;
|
23 | 24 | import java.util.stream.Collectors;
|
24 | 25 |
|
|
36 | 37 | import org.springframework.beans.factory.annotation.Value;
|
37 | 38 | import org.springframework.context.annotation.Bean;
|
38 | 39 | import org.springframework.context.annotation.Configuration;
|
| 40 | +import org.springframework.data.domain.OffsetScrollPosition; |
39 | 41 | import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
|
40 | 42 | import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories;
|
41 | 43 | import org.springframework.data.repository.query.ReactiveQueryByExampleExecutor;
|
42 | 44 | import org.springframework.graphql.BookSource;
|
43 | 45 | import org.springframework.graphql.GraphQlSetup;
|
44 | 46 | import org.springframework.graphql.ResponseHelper;
|
| 47 | +import org.springframework.graphql.data.pagination.ConnectionFieldTypeVisitor; |
45 | 48 | import org.springframework.graphql.data.query.QueryByExampleDataFetcher;
|
| 49 | +import org.springframework.graphql.data.query.ScrollPositionCursorStrategy; |
| 50 | +import org.springframework.graphql.data.query.ScrollSubrange; |
| 51 | +import org.springframework.graphql.data.query.WindowConnectionAdapter; |
| 52 | +import org.springframework.graphql.execution.ConnectionTypeDefinitionConfigurer; |
46 | 53 | import org.springframework.graphql.execution.RuntimeWiringConfigurer;
|
47 | 54 | import org.springframework.graphql.server.WebGraphQlHandler;
|
48 | 55 | import org.springframework.graphql.server.WebGraphQlRequest;
|
@@ -138,21 +145,71 @@ void shouldReactivelyFetchMultipleItems() {
|
138 | 145 | tester.accept(graphQlSetup(repository));
|
139 | 146 | }
|
140 | 147 |
|
| 148 | + @Test |
| 149 | + void shouldFetchWindow() { |
| 150 | + |
| 151 | + repository.saveAll(List.of( |
| 152 | + new Book("1", "Nineteen Eighty-Four", new Author("0", "George", "Orwell")), |
| 153 | + new Book("2", "The Great Gatsby", new Author("0", "F. Scott", "Fitzgerald")), |
| 154 | + new Book("3", "Catch-22", new Author("0", "Joseph", "Heller")), |
| 155 | + new Book("42", "Hitchhiker's Guide to the Galaxy", new Author("0", "Douglas", "Adams")), |
| 156 | + new Book("53", "Breaking Bad", new Author("0", "", "Heisenberg")))); |
| 157 | + |
| 158 | + Consumer<GraphQlSetup> tester = graphQlSetup -> { |
| 159 | + |
| 160 | + Mono<WebGraphQlResponse> response = graphQlSetup |
| 161 | + .toWebGraphQlHandler() |
| 162 | + .handleRequest(request(BookSource.booksConnectionQuery("first:2, after:\"O_3\""))); |
| 163 | + |
| 164 | + List<Map<String, Object>> edges = ResponseHelper.forResponse(response).toEntity("books.edges", List.class); |
| 165 | + assertThat(edges.size()).isEqualTo(2); |
| 166 | + assertThat(edges.get(0).get("cursor")).isEqualTo("O_4"); |
| 167 | + assertThat(edges.get(1).get("cursor")).isEqualTo("O_5"); |
| 168 | + |
| 169 | + Map<String, Object> pageInfo = ResponseHelper.forResponse(response).toEntity("books.pageInfo", Map.class); |
| 170 | + assertThat(pageInfo.size()).isEqualTo(4); |
| 171 | + assertThat(pageInfo.get("startCursor")).isEqualTo("O_4"); |
| 172 | + assertThat(pageInfo.get("endCursor")).isEqualTo("O_5"); |
| 173 | + assertThat(pageInfo.get("hasPreviousPage")).isEqualTo(true); |
| 174 | + assertThat(pageInfo.get("hasNextPage")).isEqualTo(false); |
| 175 | + }; |
| 176 | + |
| 177 | + // explicit wiring |
| 178 | + |
| 179 | + ScrollPositionCursorStrategy cursorStrategy = new ScrollPositionCursorStrategy(); |
| 180 | + |
| 181 | + DataFetcher<Mono<Iterable<Book>>> dataFetcher = |
| 182 | + QueryByExampleDataFetcher.builder(repository).cursorStrategy(cursorStrategy).scrollable(); |
| 183 | + |
| 184 | + GraphQlSetup graphQlSetup = paginationSetup(cursorStrategy).queryFetcher("books", dataFetcher); |
| 185 | + tester.accept(graphQlSetup); |
| 186 | + |
| 187 | + // auto registration |
| 188 | + graphQlSetup = paginationSetup(cursorStrategy).runtimeWiring(createRuntimeWiringConfigurer(repository)); |
| 189 | + tester.accept(graphQlSetup); |
| 190 | + } |
| 191 | + |
141 | 192 | private static GraphQlSetup graphQlSetup(String fieldName, DataFetcher<?> fetcher) {
|
142 |
| - return initGraphQlSetup(null).queryFetcher(fieldName, fetcher); |
| 193 | + return GraphQlSetup.schemaResource(BookSource.schema).queryFetcher(fieldName, fetcher); |
143 | 194 | }
|
144 | 195 |
|
145 | 196 | private static GraphQlSetup graphQlSetup(@Nullable ReactiveQueryByExampleExecutor<?> executor) {
|
146 |
| - return initGraphQlSetup(executor); |
| 197 | + return GraphQlSetup.schemaResource(BookSource.schema) |
| 198 | + .runtimeWiring(createRuntimeWiringConfigurer(executor)); |
147 | 199 | }
|
148 | 200 |
|
149 |
| - private static GraphQlSetup initGraphQlSetup(@Nullable ReactiveQueryByExampleExecutor<?> executor) { |
| 201 | + private static GraphQlSetup paginationSetup(ScrollPositionCursorStrategy cursorStrategy) { |
| 202 | + return GraphQlSetup.schemaResource(BookSource.paginationSchema) |
| 203 | + .typeDefinitionConfigurer(new ConnectionTypeDefinitionConfigurer()) |
| 204 | + .typeVisitor(ConnectionFieldTypeVisitor.create(List.of(new WindowConnectionAdapter(cursorStrategy)))); |
| 205 | + } |
150 | 206 |
|
151 |
| - RuntimeWiringConfigurer configurer = QueryByExampleDataFetcher.autoRegistrationConfigurer( |
| 207 | + private static RuntimeWiringConfigurer createRuntimeWiringConfigurer(ReactiveQueryByExampleExecutor<?> executor) { |
| 208 | + return QueryByExampleDataFetcher.autoRegistrationConfigurer( |
152 | 209 | Collections.emptyList(),
|
153 |
| - (executor != null ? Collections.singletonList(executor) : Collections.emptyList())); |
154 |
| - |
155 |
| - return GraphQlSetup.schemaResource(BookSource.schema).runtimeWiring(configurer); |
| 210 | + (executor != null ? Collections.singletonList(executor) : Collections.emptyList()), |
| 211 | + new ScrollPositionCursorStrategy(), |
| 212 | + new ScrollSubrange(OffsetScrollPosition.initial(), 10, true)); |
156 | 213 | }
|
157 | 214 |
|
158 | 215 | private WebGraphQlRequest request(String query) {
|
|
0 commit comments