獨立安裝

如果您的應用程式未使用 Spring Boot,您需要負責設定相關的 Spring for GraphQL 組件。假設您的應用程式已針對 Spring MVC 控制器進行配置,則最基本的設定將需要數個 Bean。

import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.graphql.ExecutionGraphQlService;
import org.springframework.graphql.data.method.annotation.support.AnnotatedControllerConfigurer;
import org.springframework.graphql.execution.ConnectionTypeDefinitionConfigurer;
import org.springframework.graphql.execution.DefaultBatchLoaderRegistry;
import org.springframework.graphql.execution.DefaultExecutionGraphQlService;
import org.springframework.graphql.execution.GraphQlSource;
import org.springframework.graphql.server.WebGraphQlHandler;
import org.springframework.graphql.server.webmvc.GraphQlHttpHandler;
import org.springframework.graphql.server.webmvc.GraphQlRequestPredicates;
import org.springframework.graphql.server.webmvc.GraphiQlHandler;
import org.springframework.web.servlet.function.RequestPredicate;
import org.springframework.web.servlet.function.RouterFunction;
import org.springframework.web.servlet.function.RouterFunctions;
import org.springframework.web.servlet.function.ServerResponse;

@Configuration(proxyBeanMethods = false)
public class GraphQlConfiguration {

	@Bean (1)
	public AnnotatedControllerConfigurer controllerConfigurer() {
		return new AnnotatedControllerConfigurer();
	}

	@Bean (2)
	public ExecutionGraphQlService executionGraphQlService(AnnotatedControllerConfigurer controllerConfigurer) {
		GraphQlSource graphQlSource = GraphQlSource.schemaResourceBuilder() (3)
				.schemaResources(new ClassPathResource("graphql/schema.graphqls"))
				.configureTypeDefinitions(new ConnectionTypeDefinitionConfigurer())
				.configureRuntimeWiring(controllerConfigurer)
				.exceptionResolvers(List.of(controllerConfigurer.getExceptionResolver()))
				.build();
		DefaultBatchLoaderRegistry batchLoaderRegistry = new DefaultBatchLoaderRegistry();
		DefaultExecutionGraphQlService service = new DefaultExecutionGraphQlService(graphQlSource);
		service.addDataLoaderRegistrar(batchLoaderRegistry);
		return service;
	}


	@Bean (4)
	public RouterFunction<ServerResponse> graphQlRouterFunction(ExecutionGraphQlService graphQlService) {
		WebGraphQlHandler webGraphQlHandler = WebGraphQlHandler.builder(graphQlService).build();
		GraphQlHttpHandler graphQlHttpHandler = new GraphQlHttpHandler(webGraphQlHandler);
		RequestPredicate graphQlPredicate = GraphQlRequestPredicates.graphQlHttp("/graphql");
		GraphiQlHandler graphiQlHandler = new GraphiQlHandler("/graphql", "");
		return RouterFunctions.route() (5)
				.route(graphQlPredicate, graphQlHttpHandler::handleRequest)
				.GET("/graphiql", graphiQlHandler::handleRequest)
				.build();
	}
}
1 AnnotatedControllerConfigurer Bean 負責偵測 GraphQL @Controller 處理常式。
2 ExecutionGraphQlService 以傳輸協定無關的方式處理 GraphQL 請求。
3 GraphQlSource 建構器是主要配置點。探索其 API 以取得更多選項。
4 RouterFunction 將 GraphQL 路由公開為 函數式端點
5 然後,您可以透過不同的路由公開各種傳輸協定 (WebSocket、SSE、HTTP)。

Spring for GraphQL 提供了許多其他選項,以及與 Spring 專案的整合。如需更多資訊,您可以探索 Spring Boot 自動配置