Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Commit 6ca57e2

Browse files
authored
Merge pull request #224 from graphql-java-kickstart/bugfix/219-server-endpoint-error
Bugfix/219 server endpoint error
2 parents abeb3b0 + 4769477 commit 6ca57e2

File tree

4 files changed

+54
-21
lines changed

4 files changed

+54
-21
lines changed

graphql-spring-boot-autoconfigure/src/main/java/com/oembedler/moon/graphql/boot/GraphQLWebAutoConfiguration.java

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import com.fasterxml.jackson.databind.InjectableValues;
2323
import com.fasterxml.jackson.databind.ObjectMapper;
2424
import com.oembedler.moon.graphql.boot.error.ErrorHandlerSupplier;
25-
import com.oembedler.moon.graphql.boot.error.GraphQLErrorHandlerFactory;
25+
import com.oembedler.moon.graphql.boot.error.GraphQLErrorStartupListener;
2626
import com.oembedler.moon.graphql.boot.metrics.MetricsInstrumentation;
2727
import graphql.execution.AsyncExecutionStrategy;
2828
import graphql.execution.ExecutionStrategy;
@@ -48,7 +48,6 @@
4848
import graphql.servlet.ObjectMapperConfigurer;
4949
import graphql.servlet.ObjectMapperProvider;
5050
import lombok.extern.slf4j.Slf4j;
51-
import org.springframework.beans.BeansException;
5251
import org.springframework.beans.factory.ObjectProvider;
5352
import org.springframework.beans.factory.annotation.Autowired;
5453
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
@@ -60,16 +59,14 @@
6059
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
6160
import org.springframework.boot.context.properties.EnableConfigurationProperties;
6261
import org.springframework.boot.web.servlet.ServletRegistrationBean;
63-
import org.springframework.context.ApplicationContext;
64-
import org.springframework.context.ApplicationContextAware;
65-
import org.springframework.context.ConfigurableApplicationContext;
6662
import org.springframework.context.annotation.Bean;
6763
import org.springframework.context.annotation.Configuration;
6864
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
6965
import org.springframework.web.filter.CorsFilter;
7066
import org.springframework.web.servlet.DispatcherServlet;
7167
import org.springframework.web.servlet.config.annotation.CorsRegistryWorkaround;
7268

69+
import javax.annotation.PostConstruct;
7370
import javax.servlet.MultipartConfigElement;
7471
import java.util.List;
7572
import java.util.Map;
@@ -89,7 +86,7 @@
8986
@ConditionalOnProperty(value = "graphql.servlet.enabled", havingValue = "true", matchIfMissing = true)
9087
@AutoConfigureAfter({GraphQLJavaToolsAutoConfiguration.class, JacksonAutoConfiguration.class})
9188
@EnableConfigurationProperties({GraphQLServletProperties.class})
92-
public class GraphQLWebAutoConfiguration implements ApplicationContextAware {
89+
public class GraphQLWebAutoConfiguration {
9390

9491
public static final String QUERY_EXECUTION_STRATEGY = "queryExecutionStrategy";
9592
public static final String MUTATION_EXECUTION_STRATEGY = "mutationExecutionStrategy";
@@ -130,14 +127,16 @@ public class GraphQLWebAutoConfiguration implements ApplicationContextAware {
130127
@Autowired(required = false)
131128
private BatchExecutionHandler batchExecutionHandler;
132129

133-
@Override
134-
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
135-
if (errorHandler == null) {
136-
ConfigurableApplicationContext context = (ConfigurableApplicationContext) applicationContext;
137-
errorHandler = new GraphQLErrorHandlerFactory().create(context, graphQLServletProperties.isExceptionHandlersEnabled());
138-
context.getBeanFactory().registerSingleton(errorHandler.getClass().getCanonicalName(), errorHandler);
130+
@PostConstruct
131+
void postConstruct() {
132+
if (errorHandler != null) {
133+
errorHandlerSupplier.setErrorHandler(errorHandler);
139134
}
140-
errorHandlerSupplier.setErrorHandler(errorHandler);
135+
}
136+
137+
@Bean
138+
public GraphQLErrorStartupListener graphQLErrorStartupListener() {
139+
return new GraphQLErrorStartupListener(errorHandlerSupplier, graphQLServletProperties.isExceptionHandlersEnabled());
141140
}
142141

143142
@Bean
@@ -263,12 +262,12 @@ public ObjectMapperProvider objectMapperProvider(ObjectMapper objectMapper) {
263262
@ConditionalOnMissingBean
264263
public GraphQLConfiguration graphQLServletConfiguration(GraphQLInvocationInputFactory invocationInputFactory, GraphQLQueryInvoker queryInvoker, GraphQLObjectMapper graphQLObjectMapper) {
265264
return GraphQLConfiguration.with(invocationInputFactory)
266-
.with(queryInvoker)
267-
.with(graphQLObjectMapper)
268-
.with(listeners)
269-
.with(graphQLServletProperties.isAsyncModeEnabled())
270-
.with(graphQLServletProperties.getSubscriptionTimeout())
271-
.build();
265+
.with(queryInvoker)
266+
.with(graphQLObjectMapper)
267+
.with(listeners)
268+
.with(graphQLServletProperties.isAsyncModeEnabled())
269+
.with(graphQLServletProperties.getSubscriptionTimeout())
270+
.build();
272271
}
273272

274273
@Bean

graphql-spring-boot-autoconfigure/src/main/java/com/oembedler/moon/graphql/boot/error/ErrorHandlerSupplier.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ public GraphQLErrorHandler get() {
1818
return errorHandler;
1919
}
2020

21+
public boolean isPresent() {
22+
return errorHandler != null;
23+
}
24+
2125
public void setErrorHandler(GraphQLErrorHandler errorHandler) {
2226
this.errorHandler = Objects.requireNonNull(errorHandler);
2327
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.oembedler.moon.graphql.boot.error;
2+
3+
import graphql.servlet.GraphQLErrorHandler;
4+
import org.springframework.context.ApplicationListener;
5+
import org.springframework.context.ConfigurableApplicationContext;
6+
import org.springframework.context.event.ContextRefreshedEvent;
7+
8+
import javax.validation.constraints.NotNull;
9+
10+
public class GraphQLErrorStartupListener implements ApplicationListener<ContextRefreshedEvent> {
11+
12+
private final ErrorHandlerSupplier errorHandlerSupplier;
13+
private final boolean exceptionHandlersEnabled;
14+
15+
public GraphQLErrorStartupListener(ErrorHandlerSupplier errorHandlerSupplier, boolean exceptionHandlersEnabled) {
16+
this.errorHandlerSupplier = errorHandlerSupplier;
17+
this.exceptionHandlersEnabled = exceptionHandlersEnabled;
18+
}
19+
20+
@Override
21+
public void onApplicationEvent(@NotNull ContextRefreshedEvent event) {
22+
if (!errorHandlerSupplier.isPresent()) {
23+
ConfigurableApplicationContext context = (ConfigurableApplicationContext) event.getApplicationContext();
24+
GraphQLErrorHandler errorHandler = new GraphQLErrorHandlerFactory().create(context, exceptionHandlersEnabled);
25+
context.getBeanFactory().registerSingleton(errorHandler.getClass().getCanonicalName(), errorHandler);
26+
errorHandlerSupplier.setErrorHandler(errorHandler);
27+
}
28+
}
29+
30+
}

graphql-spring-boot-test/src/main/java/com/graphql/spring/boot/test/GraphQLTestTemplate.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public void clearHeaders() {
8080
* @deprecated Use {@link #postForResource(String)} instead
8181
*
8282
* @param graphqlResource path to the classpath resource containing the GraphQL query
83-
* @return <tt>GraphQLResponse</tt> containing the result of query execution
83+
* @return GraphQLResponse containing the result of query execution
8484
* @throws IOException if the resource cannot be loaded from the classpath
8585
*/
8686
public GraphQLResponse perform(String graphqlResource) throws IOException {
@@ -97,7 +97,7 @@ public GraphQLResponse perform(String graphqlResource, ObjectNode variables) thr
9797
* Loads a GraphQL query from the given classpath resource and sends it to the GraphQL server.
9898
*
9999
* @param graphqlResource path to the classpath resource containing the GraphQL query
100-
* @return <tt>GraphQLResponse</tt> containing the result of query execution
100+
* @return GraphQLResponse containing the result of query execution
101101
* @throws IOException if the resource cannot be loaded from the classpath
102102
*/
103103
public GraphQLResponse postForResource(String graphqlResource) throws IOException {

0 commit comments

Comments
 (0)