Simplify AnnotatedDataFetcherConfigurer config
Now that we don't use JSON conversion for input arguments, we can declare AnnotatedDataFetcherConfigurer in shared config. See gh-122
This commit is contained in:
@@ -36,6 +36,7 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.support.ResourcePatternResolver;
|
||||
import org.springframework.graphql.data.method.annotation.support.AnnotatedDataFetcherConfigurer;
|
||||
import org.springframework.graphql.execution.DataFetcherExceptionResolver;
|
||||
import org.springframework.graphql.execution.GraphQlSource;
|
||||
import org.springframework.graphql.execution.MissingSchemaException;
|
||||
@@ -56,12 +57,17 @@ public class GraphQlAutoConfiguration {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(GraphQlAutoConfiguration.class);
|
||||
|
||||
@Bean
|
||||
public AnnotatedDataFetcherConfigurer annotatedDataFetcherConfigurer() {
|
||||
return new AnnotatedDataFetcherConfigurer();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public GraphQlSource graphQlSource(ResourcePatternResolver resourcePatternResolver, GraphQlProperties properties,
|
||||
ObjectProvider<DataFetcherExceptionResolver> exceptionResolversProvider,
|
||||
ObjectProvider<Instrumentation> instrumentationsProvider,
|
||||
ObjectProvider<GraphQlSourceBuilderCustomizer> sourceCustomizers,
|
||||
ObjectProvider<RuntimeWiringConfigurer> wiringConfigurers) throws IOException {
|
||||
ObjectProvider<RuntimeWiringConfigurer> wiringConfigurers) {
|
||||
|
||||
List<Resource> schemaResources = resolveSchemaResources(resourcePatternResolver, properties.getSchema().getLocations(),
|
||||
properties.getSchema().getFileExtensions());
|
||||
|
||||
@@ -36,7 +36,6 @@ import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.graphql.GraphQlService;
|
||||
import org.springframework.graphql.data.method.annotation.support.AnnotatedDataFetcherConfigurer;
|
||||
import org.springframework.graphql.execution.GraphQlSource;
|
||||
import org.springframework.graphql.web.WebGraphQlHandler;
|
||||
import org.springframework.graphql.web.WebInterceptor;
|
||||
@@ -74,13 +73,6 @@ public class GraphQlWebFluxAutoConfiguration {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(GraphQlWebFluxAutoConfiguration.class);
|
||||
|
||||
@Bean
|
||||
public AnnotatedDataFetcherConfigurer annotatedDataFetcherConfigurer(ServerCodecConfigurer configurer) {
|
||||
AnnotatedDataFetcherConfigurer dataFetcherConfigurer = new AnnotatedDataFetcherConfigurer();
|
||||
dataFetcherConfigurer.setServerCodecConfigurer(configurer);
|
||||
return dataFetcherConfigurer;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(GraphQlService.class)
|
||||
@ConditionalOnMissingBean
|
||||
|
||||
@@ -40,7 +40,6 @@ import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.graphql.GraphQlService;
|
||||
import org.springframework.graphql.data.method.annotation.support.AnnotatedDataFetcherConfigurer;
|
||||
import org.springframework.graphql.execution.GraphQlSource;
|
||||
import org.springframework.graphql.execution.ThreadLocalAccessor;
|
||||
import org.springframework.graphql.web.WebGraphQlHandler;
|
||||
@@ -82,22 +81,6 @@ public class GraphQlWebMvcAutoConfiguration {
|
||||
private static final Log logger = LogFactory.getLog(GraphQlWebMvcAutoConfiguration.class);
|
||||
|
||||
|
||||
@Bean
|
||||
public AnnotatedDataFetcherConfigurer annotatedDataFetcherConfigurer(HttpMessageConverters converters) {
|
||||
AnnotatedDataFetcherConfigurer dataFetcherConfigurer = new AnnotatedDataFetcherConfigurer();
|
||||
dataFetcherConfigurer.setJsonMessageConverter(getJsonConverter(converters));
|
||||
return dataFetcherConfigurer;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static GenericHttpMessageConverter<Object> getJsonConverter(HttpMessageConverters converters) {
|
||||
return converters.getConverters().stream()
|
||||
.filter((candidate) -> candidate.canRead(Map.class, MediaType.APPLICATION_JSON))
|
||||
.findFirst()
|
||||
.map(converter -> (GenericHttpMessageConverter<Object>) converter)
|
||||
.orElseThrow(() -> new IllegalStateException("No JSON converter"));
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(GraphQlService.class)
|
||||
@ConditionalOnMissingBean
|
||||
@@ -160,6 +143,15 @@ public class GraphQlWebMvcAutoConfiguration {
|
||||
properties.getWebsocket().getConnectionInitTimeout());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static GenericHttpMessageConverter<Object> getJsonConverter(HttpMessageConverters converters) {
|
||||
return converters.getConverters().stream()
|
||||
.filter((candidate) -> candidate.canRead(Map.class, MediaType.APPLICATION_JSON))
|
||||
.findFirst()
|
||||
.map(converter -> (GenericHttpMessageConverter<Object>) converter)
|
||||
.orElseThrow(() -> new IllegalStateException("No JSON converter"));
|
||||
}
|
||||
|
||||
@Bean
|
||||
public HandlerMapping graphQlWebSocketMapping(GraphQlWebSocketHandler handler, GraphQlProperties properties) {
|
||||
String path = properties.getWebsocket().getPath();
|
||||
|
||||
@@ -40,8 +40,6 @@ import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
import org.springframework.core.codec.Decoder;
|
||||
import org.springframework.core.codec.Encoder;
|
||||
import org.springframework.graphql.data.method.HandlerMethod;
|
||||
import org.springframework.graphql.data.method.HandlerMethodArgumentResolver;
|
||||
import org.springframework.graphql.data.method.HandlerMethodArgumentResolverComposite;
|
||||
@@ -50,11 +48,6 @@ import org.springframework.graphql.data.method.annotation.QueryMapping;
|
||||
import org.springframework.graphql.data.method.annotation.SchemaMapping;
|
||||
import org.springframework.graphql.data.method.annotation.SubscriptionMapping;
|
||||
import org.springframework.graphql.execution.RuntimeWiringConfigurer;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.codec.DecoderHttpMessageReader;
|
||||
import org.springframework.http.codec.EncoderHttpMessageWriter;
|
||||
import org.springframework.http.codec.ServerCodecConfigurer;
|
||||
import org.springframework.http.converter.GenericHttpMessageConverter;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -97,58 +90,6 @@ public class AnnotatedDataFetcherConfigurer
|
||||
@Nullable
|
||||
private HandlerMethodArgumentResolverComposite argumentResolvers;
|
||||
|
||||
@Nullable
|
||||
private GenericHttpMessageConverter<Object> jsonMessageConverter;
|
||||
|
||||
@Nullable
|
||||
private Encoder<Object> jsonEncoder;
|
||||
|
||||
@Nullable
|
||||
private Decoder<Object> jsonDecoder;
|
||||
|
||||
|
||||
/**
|
||||
* Configure the {@link org.springframework.http.converter.HttpMessageConverter}
|
||||
* to use to convert input arguments obtained from the
|
||||
* {@link DataFetchingEnvironment} and converted to the type of a declared
|
||||
* {@link org.springframework.graphql.data.method.annotation.Argument @Argument}
|
||||
* method parameter.
|
||||
* <p>This method is mutually exclusive with
|
||||
* {@link #setServerCodecConfigurer(ServerCodecConfigurer)} and is convenient
|
||||
* for use in a Spring MVC application but both variant can be used without
|
||||
* much difference.
|
||||
* @param converter the converter to use.
|
||||
*/
|
||||
public void setJsonMessageConverter(@Nullable GenericHttpMessageConverter<Object> converter) {
|
||||
this.jsonMessageConverter = converter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Variant of {@link #setJsonMessageConverter(GenericHttpMessageConverter)}
|
||||
* to use an {@link Encoder} and {@link Decoder} to convert input arguments.
|
||||
* <p>This method is mutually exclusive with
|
||||
* {@link #setJsonMessageConverter(GenericHttpMessageConverter)} and is
|
||||
* convenient for use in a Spring WebFlux application but both variant can
|
||||
* be used without much difference.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public void setServerCodecConfigurer(@Nullable ServerCodecConfigurer configurer) {
|
||||
if (configurer == null) {
|
||||
this.jsonDecoder = null;
|
||||
this.jsonEncoder = null;
|
||||
return;
|
||||
}
|
||||
this.jsonDecoder = configurer.getReaders().stream()
|
||||
.filter((reader) -> reader.canRead(MAP_RESOLVABLE_TYPE, MediaType.APPLICATION_JSON))
|
||||
.map((reader) -> ((DecoderHttpMessageReader<Object>) reader).getDecoder())
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new IllegalArgumentException("No Decoder for JSON"));
|
||||
this.jsonEncoder = configurer.getWriters().stream()
|
||||
.filter((writer) -> writer.canWrite(MAP_RESOLVABLE_TYPE, MediaType.APPLICATION_JSON))
|
||||
.map((writer) -> ((EncoderHttpMessageWriter<Object>) writer).getEncoder())
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new IllegalArgumentException("No Encoder for JSON"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) {
|
||||
|
||||
@@ -31,7 +31,6 @@ import org.springframework.graphql.data.method.annotation.MutationMapping;
|
||||
import org.springframework.graphql.data.method.annotation.QueryMapping;
|
||||
import org.springframework.graphql.data.method.annotation.SchemaMapping;
|
||||
import org.springframework.graphql.data.method.annotation.SubscriptionMapping;
|
||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||
import org.springframework.stereotype.Controller;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -84,7 +83,6 @@ public class AnnotatedDataFetcherDetectionTests {
|
||||
appContext.refresh();
|
||||
|
||||
AnnotatedDataFetcherConfigurer configurer = new AnnotatedDataFetcherConfigurer();
|
||||
configurer.setJsonMessageConverter(new MappingJackson2HttpMessageConverter());
|
||||
configurer.setApplicationContext(appContext);
|
||||
configurer.afterPropertiesSet();
|
||||
|
||||
|
||||
@@ -48,7 +48,6 @@ import org.springframework.graphql.execution.BatchLoaderRegistry;
|
||||
import org.springframework.graphql.execution.DefaultBatchLoaderRegistry;
|
||||
import org.springframework.graphql.execution.ExecutionGraphQlService;
|
||||
import org.springframework.graphql.execution.GraphQlSource;
|
||||
import org.springframework.http.codec.ServerCodecConfigurer;
|
||||
import org.springframework.stereotype.Controller;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -243,9 +242,7 @@ public class AnnotatedDataFetcherInvocationTests {
|
||||
|
||||
@Bean
|
||||
public AnnotatedDataFetcherConfigurer annotatedDataFetcherConfigurer() {
|
||||
AnnotatedDataFetcherConfigurer registrar = new AnnotatedDataFetcherConfigurer();
|
||||
registrar.setServerCodecConfigurer(ServerCodecConfigurer.create());
|
||||
return registrar;
|
||||
return new AnnotatedDataFetcherConfigurer();
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
Reference in New Issue
Block a user