Skip to content

Commit

Permalink
Fix GraphQL Java 21 compatibility issue
Browse files Browse the repository at this point in the history
Closes gh-963
  • Loading branch information
rstoyanchev committed May 6, 2024
1 parent 0ab80ee commit a323d22
Showing 1 changed file with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.graphql.execution;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiFunction;
Expand All @@ -33,6 +34,8 @@
import org.springframework.graphql.ExecutionGraphQlResponse;
import org.springframework.graphql.ExecutionGraphQlService;
import org.springframework.graphql.support.DefaultExecutionGraphQlResponse;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;

/**
* {@link ExecutionGraphQlService} that uses a {@link GraphQlSource} to obtain a
Expand All @@ -43,6 +46,9 @@
*/
public class DefaultExecutionGraphQlService implements ExecutionGraphQlService {

private static final boolean belowGraphQlJava22 = ClassUtils.isPresent(
DataLoaderRegistry21Initializer.CLASS_NAME, ExecutionGraphQlService.class.getClassLoader());

private static final BiFunction<ExecutionInput, ExecutionInput.Builder, ExecutionInput> RESET_EXECUTION_ID_CONFIGURER =
(executionInput, builder) -> builder.executionId(null).build();

Expand All @@ -55,11 +61,15 @@ public class DefaultExecutionGraphQlService implements ExecutionGraphQlService {

private final boolean isDefaultExecutionIdProvider;

private final Object emptyDataLoaderRegistryInstance;


public DefaultExecutionGraphQlService(GraphQlSource graphQlSource) {
this.graphQlSource = graphQlSource;
this.isDefaultExecutionIdProvider =
(graphQlSource.graphQl().getIdProvider() == ExecutionIdProvider.DEFAULT_EXECUTION_ID_PROVIDER);
this.emptyDataLoaderRegistryInstance = (belowGraphQlJava22) ?
DataLoaderRegistry21Initializer.getInstance() : DataLoaderRegistry22Initializer.getInstance();
}


Expand Down Expand Up @@ -105,7 +115,7 @@ public final Mono<ExecutionGraphQlResponse> execute(ExecutionGraphQlRequest requ
private ExecutionInput registerDataLoaders(ExecutionInput executionInput) {
GraphQLContext graphQLContext = executionInput.getGraphQLContext();
DataLoaderRegistry existingRegistry = executionInput.getDataLoaderRegistry();
if (existingRegistry == EmptyDataLoaderRegistryInstance.EMPTY_DATALOADER_REGISTRY) {
if (existingRegistry == this.emptyDataLoaderRegistryInstance) {
DataLoaderRegistry newRegistry = DataLoaderRegistry.newRegistry().build();
applyDataLoaderRegistrars(newRegistry, graphQLContext);
executionInput = executionInput.transform((builder) -> builder.dataLoaderRegistry(newRegistry));
Expand All @@ -120,4 +130,30 @@ private void applyDataLoaderRegistrars(DataLoaderRegistry registry, GraphQLConte
this.dataLoaderRegistrars.forEach((registrar) -> registrar.registerDataLoaders(registry, graphQLContext));
}


private static final class DataLoaderRegistry22Initializer {

public static Object getInstance() {
return EmptyDataLoaderRegistryInstance.EMPTY_DATALOADER_REGISTRY;
}
}


private static final class DataLoaderRegistry21Initializer {

public static final String CLASS_NAME =
"graphql.execution.instrumentation.dataloader.DataLoaderDispatcherInstrumentationState";

@SuppressWarnings("DataFlowIssue")
public static Object getInstance() {
try {
Field field = ReflectionUtils.findField(Class.forName(CLASS_NAME), "EMPTY_DATALOADER_REGISTRY");
return ReflectionUtils.getField(field, null);
}
catch (ClassNotFoundException ex) {
throw new RuntimeException(ex);
}
}
}

}

0 comments on commit a323d22

Please sign in to comment.