如果您的应用程序未使用 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 | 该 bean 负责检测 GraphQL 处理程序。AnnotatedControllerConfigurer @Controller |
2 | 它以与传输无关的方式处理 GraphQL 请求。ExecutionGraphQlService |
3 | 构建器是主要配置点。探索其 API 以获取更多选项。GraphQlSource |
4 | 将 GraphQL 路由公开为功能终端节点。RouterFunction |
5 | 然后,您可以通过不同的路由公开各种传输方式(WebSocket、SSE、HTTP)。 |
1 | 该 bean 负责检测 GraphQL 处理程序。AnnotatedControllerConfigurer @Controller |
2 | 它以与传输无关的方式处理 GraphQL 请求。ExecutionGraphQlService |
3 | 构建器是主要配置点。探索其 API 以获取更多选项。GraphQlSource |
4 | 将 GraphQL 路由公开为功能终端节点。RouterFunction |
5 | 然后,您可以通过不同的路由公开各种传输方式(WebSocket、SSE、HTTP)。 |
Spring for GraphQL 提供了许多其他选项以及与 Spring 项目的集成。 有关更多信息,您可以探索 Spring Boot 自动配置。