34
34
import org .springframework .graphql .ExecutionGraphQlResponse ;
35
35
import org .springframework .graphql .ExecutionGraphQlService ;
36
36
import org .springframework .graphql .support .DefaultExecutionGraphQlResponse ;
37
+ import org .springframework .lang .Nullable ;
37
38
import org .springframework .util .ClassUtils ;
38
39
import org .springframework .util .ReflectionUtils ;
39
40
@@ -57,7 +58,8 @@ public class DefaultExecutionGraphQlService implements ExecutionGraphQlService {
57
58
58
59
private final List <DataLoaderRegistrar > dataLoaderRegistrars = new ArrayList <>();
59
60
60
- private boolean hasDataLoaderRegistrations ;
61
+ @ Nullable
62
+ private Boolean hasDataLoaderRegistrations ;
61
63
62
64
private final boolean isDefaultExecutionIdProvider ;
63
65
@@ -80,13 +82,6 @@ public DefaultExecutionGraphQlService(GraphQlSource graphQlSource) {
80
82
*/
81
83
public void addDataLoaderRegistrar (DataLoaderRegistrar registrar ) {
82
84
this .dataLoaderRegistrars .add (registrar );
83
- this .hasDataLoaderRegistrations = (this .hasDataLoaderRegistrations || hasRegistrations (registrar ));
84
- }
85
-
86
- private static boolean hasRegistrations (DataLoaderRegistrar registrar ) {
87
- DataLoaderRegistry registry = DataLoaderRegistry .newRegistry ().build ();
88
- registrar .registerDataLoaders (registry , GraphQLContext .newContext ().build ());
89
- return !registry .getDataLoaders ().isEmpty ();
90
85
}
91
86
92
87
@@ -104,28 +99,41 @@ public final Mono<ExecutionGraphQlResponse> execute(ExecutionGraphQlRequest requ
104
99
ContextSnapshotFactoryHelper .saveInstance (factory , graphQLContext );
105
100
factory .captureFrom (contextView ).updateContext (graphQLContext );
106
101
107
- ExecutionInput updatedExecutionInput =
108
- (this .hasDataLoaderRegistrations ? registerDataLoaders (executionInput ) : executionInput );
102
+ ExecutionInput executionInputToUse = registerDataLoaders (executionInput );
109
103
110
- return Mono .fromFuture (this .graphQlSource .graphQl ().executeAsync (updatedExecutionInput ))
111
- .map ((result ) -> new DefaultExecutionGraphQlResponse (updatedExecutionInput , result ));
104
+ return Mono .fromFuture (this .graphQlSource .graphQl ().executeAsync (executionInputToUse ))
105
+ .map ((result ) -> new DefaultExecutionGraphQlResponse (executionInputToUse , result ));
112
106
});
113
107
}
114
108
115
109
private ExecutionInput registerDataLoaders (ExecutionInput executionInput ) {
116
- GraphQLContext graphQLContext = executionInput .getGraphQLContext ();
117
- DataLoaderRegistry existingRegistry = executionInput .getDataLoaderRegistry ();
118
- if (existingRegistry == this .emptyDataLoaderRegistryInstance ) {
119
- DataLoaderRegistry newRegistry = DataLoaderRegistry .newRegistry ().build ();
120
- applyDataLoaderRegistrars (newRegistry , graphQLContext );
121
- executionInput = executionInput .transform ((builder ) -> builder .dataLoaderRegistry (newRegistry ));
110
+ if (this .hasDataLoaderRegistrations == null ) {
111
+ this .hasDataLoaderRegistrations = initHasDataLoaderRegistrations ();
122
112
}
123
- else {
124
- applyDataLoaderRegistrars (existingRegistry , graphQLContext );
113
+ if (this .hasDataLoaderRegistrations ) {
114
+ GraphQLContext graphQLContext = executionInput .getGraphQLContext ();
115
+ DataLoaderRegistry existingRegistry = executionInput .getDataLoaderRegistry ();
116
+ if (existingRegistry == this .emptyDataLoaderRegistryInstance ) {
117
+ DataLoaderRegistry newRegistry = DataLoaderRegistry .newRegistry ().build ();
118
+ applyDataLoaderRegistrars (newRegistry , graphQLContext );
119
+ executionInput = executionInput .transform ((builder ) -> builder .dataLoaderRegistry (newRegistry ));
120
+ }
121
+ else {
122
+ applyDataLoaderRegistrars (existingRegistry , graphQLContext );
123
+ }
125
124
}
126
125
return executionInput ;
127
126
}
128
127
128
+ private boolean initHasDataLoaderRegistrations () {
129
+ for (DataLoaderRegistrar registrar : this .dataLoaderRegistrars ) {
130
+ if (registrar .hasRegistrations ()) {
131
+ return true ;
132
+ }
133
+ }
134
+ return false ;
135
+ }
136
+
129
137
private void applyDataLoaderRegistrars (DataLoaderRegistry registry , GraphQLContext graphQLContext ) {
130
138
this .dataLoaderRegistrars .forEach ((registrar ) -> registrar .registerDataLoaders (registry , graphQLContext ));
131
139
}
0 commit comments