Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ public GraphQLErrorStartupListener(ErrorHandlerSupplier errorHandlerSupplier, bo

@Override
public void onApplicationEvent(@NonNull ApplicationReadyEvent event) {
ConfigurableApplicationContext context = event.getApplicationContext();
GraphQLErrorHandler errorHandler = new GraphQLErrorHandlerFactory().create(context, exceptionHandlersEnabled);
context.getBeanFactory().registerSingleton(errorHandler.getClass().getCanonicalName(), errorHandler);
errorHandlerSupplier.setErrorHandler(errorHandler);
if (!errorHandlerSupplier.isPresent()) {
ConfigurableApplicationContext context = event.getApplicationContext();
GraphQLErrorHandler errorHandler = new GraphQLErrorHandlerFactory().create(context, exceptionHandlersEnabled);
context.getBeanFactory().registerSingleton(errorHandler.getClass().getCanonicalName(), errorHandler);
errorHandlerSupplier.setErrorHandler(errorHandler);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package graphql.kickstart.spring.error;

import graphql.kickstart.execution.error.GraphQLErrorHandler;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

public class GraphQLErrorStartupListenerTest {

@Test
void error_handler_is_not_overridden_when_present() {
GraphQLErrorHandler expectedErrorHandler = Mockito.mock(GraphQLErrorHandler.class);
ErrorHandlerSupplier errorHandlerSupplier = new ErrorHandlerSupplier(expectedErrorHandler);
GraphQLErrorStartupListener graphQLErrorStartupListener = new GraphQLErrorStartupListener(errorHandlerSupplier, false);
graphQLErrorStartupListener.onApplicationEvent(getApplicationReadyEvent());
Assertions.assertThat(errorHandlerSupplier.get()).isEqualTo(expectedErrorHandler);
}

@Test
void error_handler_is_set_when_not_present() {
ErrorHandlerSupplier errorHandlerSupplier = new ErrorHandlerSupplier(null);
GraphQLErrorStartupListener graphQLErrorStartupListener = new GraphQLErrorStartupListener(errorHandlerSupplier, false);
graphQLErrorStartupListener.onApplicationEvent(getApplicationReadyEvent());
Assertions.assertThat(errorHandlerSupplier.get()).isNotNull();
}

private ApplicationReadyEvent getApplicationReadyEvent() {
AnnotationConfigWebApplicationContext annotationConfigWebApplicationContext = new AnnotationConfigWebApplicationContext();
annotationConfigWebApplicationContext.refresh();
return new ApplicationReadyEvent(new SpringApplication(), new String[0], annotationConfigWebApplicationContext);
}

}