Skip to content

Commit c2f0b54

Browse files
committed
Allow custom DataLoaderRegistry
Closes gh-836
1 parent 4912588 commit c2f0b54

File tree

2 files changed

+80
-4
lines changed

2 files changed

+80
-4
lines changed

spring-graphql/src/main/java/org/springframework/graphql/execution/DefaultExecutionGraphQlService.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import graphql.GraphQL;
2525
import graphql.GraphQLContext;
2626
import graphql.execution.ExecutionIdProvider;
27+
import graphql.execution.instrumentation.dataloader.DataLoaderDispatcherInstrumentationState;
2728
import io.micrometer.context.ContextSnapshot;
2829
import org.dataloader.DataLoaderRegistry;
2930
import reactor.core.publisher.Mono;
@@ -88,12 +89,21 @@ public final Mono<ExecutionGraphQlResponse> execute(ExecutionGraphQlRequest requ
8889
private ExecutionInput registerDataLoaders(ExecutionInput executionInput) {
8990
if (!this.dataLoaderRegistrars.isEmpty()) {
9091
GraphQLContext graphQLContext = executionInput.getGraphQLContext();
91-
DataLoaderRegistry previousRegistry = executionInput.getDataLoaderRegistry();
92-
DataLoaderRegistry newRegistry = DataLoaderRegistry.newRegistry().registerAll(previousRegistry).build();
93-
this.dataLoaderRegistrars.forEach(registrar -> registrar.registerDataLoaders(newRegistry, graphQLContext));
94-
executionInput = executionInput.transform(builder -> builder.dataLoaderRegistry(newRegistry));
92+
DataLoaderRegistry existingRegistry = executionInput.getDataLoaderRegistry();
93+
if (existingRegistry == DataLoaderDispatcherInstrumentationState.EMPTY_DATALOADER_REGISTRY) {
94+
DataLoaderRegistry newRegistry = DataLoaderRegistry.newRegistry().build();
95+
applyDataLoaderRegistrars(newRegistry, graphQLContext);
96+
executionInput = executionInput.transform(builder -> builder.dataLoaderRegistry(newRegistry));
97+
}
98+
else {
99+
applyDataLoaderRegistrars(existingRegistry, graphQLContext);
100+
}
95101
}
96102
return executionInput;
97103
}
98104

105+
private void applyDataLoaderRegistrars(DataLoaderRegistry registry, GraphQLContext graphQLContext) {
106+
this.dataLoaderRegistrars.forEach(registrar -> registrar.registerDataLoaders(registry, graphQLContext));
107+
}
108+
99109
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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

Comments
 (0)