16
16
17
17
package org .springframework .graphql .execution ;
18
18
19
+ import java .lang .reflect .Field ;
19
20
import java .util .ArrayList ;
20
21
import java .util .List ;
21
22
import java .util .function .BiFunction ;
33
34
import org .springframework .graphql .ExecutionGraphQlResponse ;
34
35
import org .springframework .graphql .ExecutionGraphQlService ;
35
36
import org .springframework .graphql .support .DefaultExecutionGraphQlResponse ;
37
+ import org .springframework .util .ClassUtils ;
38
+ import org .springframework .util .ReflectionUtils ;
36
39
37
40
/**
38
41
* {@link ExecutionGraphQlService} that uses a {@link GraphQlSource} to obtain a
43
46
*/
44
47
public class DefaultExecutionGraphQlService implements ExecutionGraphQlService {
45
48
49
+ private static final boolean belowGraphQlJava22 = ClassUtils .isPresent (
50
+ DataLoaderRegistry21Initializer .CLASS_NAME , ExecutionGraphQlService .class .getClassLoader ());
51
+
46
52
private static final BiFunction <ExecutionInput , ExecutionInput .Builder , ExecutionInput > RESET_EXECUTION_ID_CONFIGURER =
47
53
(executionInput , builder ) -> builder .executionId (null ).build ();
48
54
@@ -55,11 +61,15 @@ public class DefaultExecutionGraphQlService implements ExecutionGraphQlService {
55
61
56
62
private final boolean isDefaultExecutionIdProvider ;
57
63
64
+ private final Object emptyDataLoaderRegistryInstance ;
65
+
58
66
59
67
public DefaultExecutionGraphQlService (GraphQlSource graphQlSource ) {
60
68
this .graphQlSource = graphQlSource ;
61
69
this .isDefaultExecutionIdProvider =
62
70
(graphQlSource .graphQl ().getIdProvider () == ExecutionIdProvider .DEFAULT_EXECUTION_ID_PROVIDER );
71
+ this .emptyDataLoaderRegistryInstance = (belowGraphQlJava22 ) ?
72
+ DataLoaderRegistry21Initializer .getInstance () : DataLoaderRegistry22Initializer .getInstance ();
63
73
}
64
74
65
75
@@ -105,7 +115,7 @@ public final Mono<ExecutionGraphQlResponse> execute(ExecutionGraphQlRequest requ
105
115
private ExecutionInput registerDataLoaders (ExecutionInput executionInput ) {
106
116
GraphQLContext graphQLContext = executionInput .getGraphQLContext ();
107
117
DataLoaderRegistry existingRegistry = executionInput .getDataLoaderRegistry ();
108
- if (existingRegistry == EmptyDataLoaderRegistryInstance . EMPTY_DATALOADER_REGISTRY ) {
118
+ if (existingRegistry == this . emptyDataLoaderRegistryInstance ) {
109
119
DataLoaderRegistry newRegistry = DataLoaderRegistry .newRegistry ().build ();
110
120
applyDataLoaderRegistrars (newRegistry , graphQLContext );
111
121
executionInput = executionInput .transform ((builder ) -> builder .dataLoaderRegistry (newRegistry ));
@@ -120,4 +130,30 @@ private void applyDataLoaderRegistrars(DataLoaderRegistry registry, GraphQLConte
120
130
this .dataLoaderRegistrars .forEach ((registrar ) -> registrar .registerDataLoaders (registry , graphQLContext ));
121
131
}
122
132
133
+
134
+ private static final class DataLoaderRegistry22Initializer {
135
+
136
+ public static Object getInstance () {
137
+ return EmptyDataLoaderRegistryInstance .EMPTY_DATALOADER_REGISTRY ;
138
+ }
139
+ }
140
+
141
+
142
+ private static final class DataLoaderRegistry21Initializer {
143
+
144
+ public static final String CLASS_NAME =
145
+ "graphql.execution.instrumentation.dataloader.DataLoaderDispatcherInstrumentationState" ;
146
+
147
+ @ SuppressWarnings ("DataFlowIssue" )
148
+ public static Object getInstance () {
149
+ try {
150
+ Field field = ReflectionUtils .findField (Class .forName (CLASS_NAME ), "EMPTY_DATALOADER_REGISTRY" );
151
+ return ReflectionUtils .getField (field , null );
152
+ }
153
+ catch (ClassNotFoundException ex ) {
154
+ throw new RuntimeException (ex );
155
+ }
156
+ }
157
+ }
158
+
123
159
}
0 commit comments