|
| 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 | + |
| 17 | +package org.springframework.graphql.execution; |
| 18 | + |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +import org.dataloader.DataLoaderRegistry; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import reactor.core.publisher.Flux; |
| 24 | + |
| 25 | +import org.springframework.graphql.Author; |
| 26 | +import org.springframework.graphql.Book; |
| 27 | +import org.springframework.graphql.ExecutionGraphQlRequest; |
| 28 | +import org.springframework.graphql.ExecutionGraphQlResponse; |
| 29 | +import org.springframework.graphql.GraphQlSetup; |
| 30 | +import org.springframework.graphql.TestExecutionRequest; |
| 31 | + |
| 32 | +import static org.assertj.core.api.Assertions.assertThat; |
| 33 | + |
| 34 | +/** |
| 35 | + * Unit tests for {@link DefaultExecutionGraphQlService}. |
| 36 | + * |
| 37 | + * @author Rossen Stoyanchev |
| 38 | + * @since 1.2.4 |
| 39 | + */ |
| 40 | +public class DefaultExecutionGraphQlServiceTests { |
| 41 | + |
| 42 | + @Test |
| 43 | + void customDataLoaderRegistry() { |
| 44 | + DefaultBatchLoaderRegistry batchLoaderRegistry = new DefaultBatchLoaderRegistry(); |
| 45 | + batchLoaderRegistry.forTypePair(Book.class, Author.class) |
| 46 | + .registerBatchLoader((books, batchLoaderEnvironment) -> Flux.empty()); |
| 47 | + |
| 48 | + GraphQlSource graphQlSource = GraphQlSetup.schemaContent("type Query { greeting: String }") |
| 49 | + .queryFetcher("greeting", (env) -> "hi") |
| 50 | + .toGraphQlSource(); |
| 51 | + |
| 52 | + DefaultExecutionGraphQlService graphQlService = new DefaultExecutionGraphQlService(graphQlSource); |
| 53 | + graphQlService.addDataLoaderRegistrar(batchLoaderRegistry); |
| 54 | + |
| 55 | + DataLoaderRegistry myRegistry = new DataLoaderRegistry(); |
| 56 | + |
| 57 | + ExecutionGraphQlRequest request = TestExecutionRequest.forDocument("{ greeting }"); |
| 58 | + request.configureExecutionInput((input, builder) -> builder.dataLoaderRegistry(myRegistry).build()); |
| 59 | + |
| 60 | + ExecutionGraphQlResponse response = graphQlService.execute(request).block(); |
| 61 | + Map<?, ?> data = response.getExecutionResult().getData(); |
| 62 | + assertThat(data).isEqualTo(Map.of("greeting", "hi")); |
| 63 | + assertThat(myRegistry.getDataLoaders()).hasSize(1); |
| 64 | + } |
| 65 | + |
| 66 | +} |
0 commit comments