All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.xlrit.gears.server.graphql.GraphqlConfiguration Maven / Gradle / Ivy

There is a newer version: 1.17.6
Show newest version
package com.xlrit.gears.server.graphql;

import jakarta.persistence.EntityManager;

import com.xlrit.gears.base.model.User;
import com.xlrit.gears.engine.error.InputErrors;
import com.xlrit.gears.engine.facade.InProgress;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.flowable.engine.runtime.ProcessInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.graphql.GraphQlProperties;
import org.springframework.boot.autoconfigure.graphql.GraphQlSourceBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.graphql.execution.BatchLoaderRegistry;
import org.springframework.graphql.execution.ClassNameTypeResolver;
import org.springframework.graphql.execution.RuntimeWiringConfigurer;
import org.springframework.graphql.server.WebGraphQlHandler;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.function.*;

import static com.xlrit.gears.server.graphql.GraphqlMultipartHandler.SUPPORTED_RESPONSE_MEDIA_TYPES;
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA;

@Configuration
public class GraphqlConfiguration {
	private static final Logger LOG = LoggerFactory.getLogger(GraphqlConfiguration.class);

	public GraphqlConfiguration(BatchLoaderRegistry registry, EntityManager entityManager) {
		new EntityBatchLoader<>(entityManager, User.class).registerSelf(registry);
	}

	@Bean
	public RuntimeWiringConfigurer runtimeWiringConfigurer(ObjectMapper objectMapper) {
		return wiringBuilder -> wiringBuilder
			.scalar(CustomScalars.json(objectMapper))
			.scalar(CustomScalars.upload())
			.scalar(OffsetDateTimeScalar.INSTANCE);
	}

	@Bean
	public GraphQlSourceBuilderCustomizer sourceBuilderCustomizer() {
		ClassNameTypeResolver typeResolver = new ClassNameTypeResolver();

		// union SubmitResult = SubmitInProgress | ProcessInstance | InputErrors
		typeResolver.addMapping(InProgress.class, "SubmitInProgress");
		typeResolver.addMapping(ProcessInstance.class, "ProcessInstance");
		typeResolver.addMapping(InputErrors.class, "InputErrors");

		return builder -> builder
			.defaultTypeResolver(typeResolver)
			.inspectSchemaMappings(schemaReport -> LOG.info("{}", schemaReport));
	}

	@Bean
	@Order(1)
	public RouterFunction graphQlMultipartRouterFunction(
		GraphQlProperties properties,
		WebGraphQlHandler webGraphQlHandler,
		ObjectMapper objectMapper
	) {
		String path = properties.getPath();
		GraphqlMultipartHandler graphqlMultipartHandler = new GraphqlMultipartHandler(webGraphQlHandler, objectMapper);
		RequestPredicate predicate = RequestPredicates.contentType(MULTIPART_FORM_DATA)
			.and(RequestPredicates.accept(SUPPORTED_RESPONSE_MEDIA_TYPES.toArray(MediaType[]::new)));
		return RouterFunctions.route()
			.POST(path, predicate, graphqlMultipartHandler::handleRequest)
			.build();
	}
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy