此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring GraphQL 1.4.1spring-doc.cadn.net.cn

图形QL

GraphiQL 是一个图形交互式浏览器内 GraphQL IDE。 它在开发人员中非常受欢迎,因为它可以轻松探索和交互式开发 GraphQL API。 在开发过程中,库存的 GraphiQL 集成通常足以帮助开发人员处理 API。 在生产中,应用程序可能需要自定义的 GraphiQL 版本,该版本附带公司徽标或特定的身份验证支持。spring-doc.cadn.net.cn

Spring for GraphQL 附带库存 GraphiQLindex.html使用托管在 esm.sh CDN 上的静态资源。 Spring Boot 应用程序可以使用配置属性轻松启用此页面spring-doc.cadn.net.cn

如果您的应用程序需要不依赖于 CDN 的设置,或者您希望自定义用户界面,则可能需要自定义 GraphiQL 构建。 这可以通过两个步骤完成:spring-doc.cadn.net.cn

  1. 配置和编译 GraphiQL 构建spring-doc.cadn.net.cn

  2. 通过 Spring Web 基础设施公开构建的 GraphiQL 实例spring-doc.cadn.net.cn

创建自定义 GraphiQL 构建

这部分通常不在本文档的范围之内,因为自定义构建有多个选项。 您可以在官方 GraphiQL 文档中找到更多信息。 您可以选择将构建结果直接复制到应用程序资源中。 或者,您可以利用 GradleMaven 构建插件将 JavaScript 构建作为单独Node.js模块集成到项目中。spring-doc.cadn.net.cn

公开 GraphiQL 实例

一旦 GraphiQL 构建在类路径上可用,您就可以将其公开为具有功能性 Web 框架的端点。spring-doc.cadn.net.cn

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.ClassPathResource;
import org.springframework.graphql.server.webmvc.GraphiQlHandler;
import org.springframework.web.servlet.function.RouterFunction;
import org.springframework.web.servlet.function.RouterFunctions;
import org.springframework.web.servlet.function.ServerResponse;

@Configuration
public class GraphiQlConfiguration {

	@Bean
	@Order(0)
	public RouterFunction<ServerResponse> graphiQlRouterFunction() {
		RouterFunctions.Builder builder = RouterFunctions.route();
		ClassPathResource graphiQlPage = new ClassPathResource("graphiql/index.html"); (1)
		GraphiQlHandler graphiQLHandler = new GraphiQlHandler("/graphql", "", graphiQlPage); (2)
		builder = builder.GET("/graphiql", graphiQLHandler::handleRequest); (3)
		return builder.build(); (4)
	}
}
1 从类路径加载 GraphiQL 页面(在这里,我们使用的是 Spring 附带的 GraphQL 版本)
2 配置用于处理 HTTP 请求的 Web 处理程序;您可以实现自定义HandlerFunction取决于您的用例
3 最后,将处理程序映射到特定的 HTTP 端点
4 通过RouterFunction

您可能还需要配置应用程序以提供相关的静态资源spring-doc.cadn.net.cn