|
此版本仍在开发中,尚未被认为是稳定的。请使用最新稳定版本 Spring GraphQL 2.0.2! |
独立安装设置
如果您的应用程序未使用 Spring Boot,则需要自行设置相关的 Spring 用于 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 | The ExecutionGraphQlService 处理GraphQL请求,采用传输无关的方式。 |
| 3 | GraphQlSource构建器是主要的配置点。探索其API以获取更多选项。 |
| 4 | The RouterFunction 暴露了GraphQL路由为功能端点。 |
| 5 | 然后您可以暴露各种传输(WebSocket、SSE、HTTP)到不同的路由。 |
Spring for GraphQL 提供了其他许多选项和与 Spring 项目的集成。 要了解更多信息,请参阅 Spring Boot 自动配置。