Skip to content

Commit

Permalink
Polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Apr 28, 2022
1 parent a94a064 commit 54b1396
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,63 +132,61 @@ public void addFormatterRegistrar(FormatterRegistrar registrar) {
registrar.registerFormatters(this.conversionService);
}

/**
* Configure the {@link ConversionService} used for binding handler arguments.
* @deprecated in favor of using {@link #addFormatterRegistrar(FormatterRegistrar)}
* to customize the built-in ConversionService instance.
*/
@Deprecated
public void setConversionService(ConversionService conversionService) {
Assert.isInstanceOf(FormattingConversionService.class, conversionService);
this.conversionService = (FormattingConversionService) conversionService;
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}

protected final ApplicationContext obtainApplicationContext() {
Assert.state(this.applicationContext != null, "No ApplicationContext");
return this.applicationContext;
}


@Override
public void afterPropertiesSet() {
this.argumentResolvers = new HandlerMethodArgumentResolverComposite();

this.argumentResolvers = initArgumentResolvers();

if (beanValidationPresent) {
this.validator = HandlerMethodInputValidatorFactory.create(obtainApplicationContext());
}
}

private HandlerMethodArgumentResolverComposite initArgumentResolvers() {

HandlerMethodArgumentResolverComposite resolvers = new HandlerMethodArgumentResolverComposite();

// Annotation based
if (springDataPresent) {
// Must be ahead of ArgumentMethodArgumentResolver
this.argumentResolvers.addResolver(new ProjectedPayloadMethodArgumentResolver(obtainApplicationContext()));
resolvers.addResolver(new ProjectedPayloadMethodArgumentResolver(obtainApplicationContext()));
}
this.argumentResolvers.addResolver(new ArgumentMapMethodArgumentResolver());
resolvers.addResolver(new ArgumentMapMethodArgumentResolver());
GraphQlArgumentBinder argumentBinder = new GraphQlArgumentBinder(this.conversionService);
this.argumentResolvers.addResolver(new ArgumentMethodArgumentResolver(argumentBinder));
this.argumentResolvers.addResolver(new ArgumentsMethodArgumentResolver(argumentBinder));
this.argumentResolvers.addResolver(new ContextValueMethodArgumentResolver());
resolvers.addResolver(new ArgumentMethodArgumentResolver(argumentBinder));
resolvers.addResolver(new ArgumentsMethodArgumentResolver(argumentBinder));
resolvers.addResolver(new ContextValueMethodArgumentResolver());

// Type based
this.argumentResolvers.addResolver(new DataFetchingEnvironmentMethodArgumentResolver());
this.argumentResolvers.addResolver(new DataLoaderMethodArgumentResolver());
resolvers.addResolver(new DataFetchingEnvironmentMethodArgumentResolver());
resolvers.addResolver(new DataLoaderMethodArgumentResolver());
if (springSecurityPresent) {
this.argumentResolvers.addResolver(new PrincipalMethodArgumentResolver());
resolvers.addResolver(new PrincipalMethodArgumentResolver());
BeanResolver beanResolver = new BeanFactoryResolver(obtainApplicationContext());
this.argumentResolvers.addResolver(new AuthenticationPrincipalArgumentResolver(beanResolver));
resolvers.addResolver(new AuthenticationPrincipalArgumentResolver(beanResolver));
}
if (KotlinDetector.isKotlinPresent()) {
this.argumentResolvers.addResolver(new ContinuationHandlerMethodArgumentResolver());
resolvers.addResolver(new ContinuationHandlerMethodArgumentResolver());
}

// This works as a fallback, after other resolvers
this.argumentResolvers.addResolver(new SourceMethodArgumentResolver());
// This works as a fallback, after all other resolvers
resolvers.addResolver(new SourceMethodArgumentResolver());

if (beanValidationPresent) {
this.validator = HandlerMethodInputValidatorFactory.create(obtainApplicationContext());
}
return resolvers;
}

protected final ApplicationContext obtainApplicationContext() {
Assert.state(this.applicationContext != null, "No ApplicationContext");
return this.applicationContext;
}


@Override
public void configure(RuntimeWiring.Builder runtimeWiringBuilder) {
Assert.state(this.argumentResolvers != null, "`argumentResolvers` is not initialized");
Expand Down Expand Up @@ -251,8 +249,8 @@ private Collection<MappingInfo> findHandlerMethods(Object handler, @Nullable Cla
}

Class<?> userClass = ClassUtils.getUserClass(handlerClass);
Map<Method, MappingInfo> map =
MethodIntrospector.selectMethods(userClass, (Method method) -> getMappingInfo(method, handler, userClass));
Map<Method, MappingInfo> map = MethodIntrospector.selectMethods(
userClass, (Method method) -> getMappingInfo(method, handler, userClass));

Collection<MappingInfo> mappingInfos = map.values();

Expand Down Expand Up @@ -330,10 +328,10 @@ private MappingInfo getMappingInfo(Method method, Object handler, Class<?> handl
}

private HandlerMethod createHandlerMethod(Method method, Object handler, Class<?> handlerType) {
Method invocableMethod = AopUtils.selectInvocableMethod(method, handlerType);
Method theMethod = AopUtils.selectInvocableMethod(method, handlerType);
return (handler instanceof String ?
new HandlerMethod((String) handler, obtainApplicationContext().getAutowireCapableBeanFactory(), invocableMethod) :
new HandlerMethod(handler, invocableMethod));
new HandlerMethod((String) handler, obtainApplicationContext().getAutowireCapableBeanFactory(), theMethod) :
new HandlerMethod(handler, theMethod));
}

private String formatMappings(Class<?> handlerType, Collection<MappingInfo> infos) {
Expand Down Expand Up @@ -389,12 +387,11 @@ public void configure(GraphQLCodeRegistry.Builder codeRegistryBuilder) {
configure(wiringBuilder);
RuntimeWiring runtimeWiring = wiringBuilder.build();

runtimeWiring.getDataFetchers().forEach((typeName, dataFetcherMap) -> {
dataFetcherMap.forEach((key, value) -> {
FieldCoordinates coordinates = FieldCoordinates.coordinates(typeName, key);
codeRegistryBuilder.dataFetcher(coordinates, (DataFetcher<?>) value);
});
});
runtimeWiring.getDataFetchers().forEach((typeName, dataFetcherMap) ->
dataFetcherMap.forEach((key, value) -> {
FieldCoordinates coordinates = FieldCoordinates.coordinates(typeName, key);
codeRegistryBuilder.dataFetcher(coordinates, (DataFetcher<?>) value);
}));
}


Expand All @@ -416,6 +413,7 @@ public FieldCoordinates getCoordinates() {
return this.coordinates;
}

@SuppressWarnings("BooleanMethodIsAlwaysInverted")
public boolean isBatchMapping() {
return this.batchMapping;
}
Expand Down Expand Up @@ -445,8 +443,10 @@ static class SchemaMappingDataFetcher implements DataFetcher<Object> {

private final boolean subscription;

public SchemaMappingDataFetcher(MappingInfo info, HandlerMethodArgumentResolverComposite resolvers,
public SchemaMappingDataFetcher(
MappingInfo info, HandlerMethodArgumentResolverComposite resolvers,
@Nullable HandlerMethodInputValidator validator) {

this.info = info;
this.argumentResolvers = resolvers;
this.validator = validator;
Expand All @@ -471,7 +471,11 @@ public HandlerMethod getHandlerMethod() {
@Override
@SuppressWarnings("ConstantConditions")
public Object get(DataFetchingEnvironment environment) throws Exception {
return new DataFetcherHandlerMethod(getHandlerMethod(), this.argumentResolvers, this.validator, this.subscription).invoke(environment);

DataFetcherHandlerMethod handlerMethod = new DataFetcherHandlerMethod(
getHandlerMethod(), this.argumentResolvers, this.validator, this.subscription);

return handlerMethod.invoke(environment);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ public class GraphQlHttpHandler {
private static final Log logger = LogFactory.getLog(GraphQlHttpHandler.class);

private static final ParameterizedTypeReference<Map<String, Object>> MAP_PARAMETERIZED_TYPE_REF =
new ParameterizedTypeReference<Map<String, Object>>() {
};
new ParameterizedTypeReference<Map<String, Object>>() {};

private static final List<MediaType> SUPPORTED_MEDIA_TYPES = Arrays.asList(MediaType.APPLICATION_GRAPHQL, MediaType.APPLICATION_JSON);
private static final List<MediaType> SUPPORTED_MEDIA_TYPES =
Arrays.asList(MediaType.APPLICATION_GRAPHQL, MediaType.APPLICATION_JSON);

private final WebGraphQlHandler graphQlHandler;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ public class GraphQlHttpHandler {
private static final Log logger = LogFactory.getLog(GraphQlHttpHandler.class);

private static final ParameterizedTypeReference<Map<String, Object>> MAP_PARAMETERIZED_TYPE_REF =
new ParameterizedTypeReference<Map<String, Object>>() {
};
new ParameterizedTypeReference<Map<String, Object>>() {};

private static final List<MediaType> SUPPORTED_MEDIA_TYPES = Arrays.asList(MediaType.APPLICATION_GRAPHQL, MediaType.APPLICATION_JSON);
private static final List<MediaType> SUPPORTED_MEDIA_TYPES =
Arrays.asList(MediaType.APPLICATION_GRAPHQL, MediaType.APPLICATION_JSON);

private final IdGenerator idGenerator = new AlternativeJdkIdGenerator();

Expand Down

0 comments on commit 54b1396

Please sign in to comment.