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

适用于 GraphQL 的 Spring

如果要构建 GraphQL 应用程序,可以利用 Spring Boot 对 Spring for GraphQL 的自动配置。 Spring for GraphQL 项目基于 GraphQL Java。 您将需要spring-boot-starter-graphqlstarter 的最小值。 由于 GraphQL 与传输无关,因此您还需要在应用程序中添加一个或多个额外的Starters,以便在 Web 上公开 GraphQL API:spring-doc.cadn.net.cn

起动机 运输 实现

spring-boot-starter-webspring-doc.cadn.net.cn

HTTP 协议spring-doc.cadn.net.cn

Spring MVCspring-doc.cadn.net.cn

spring-boot-starter-websocketspring-doc.cadn.net.cn

WebSocket 浏览器spring-doc.cadn.net.cn

用于 Servlet 应用程序的 WebSocketspring-doc.cadn.net.cn

spring-boot-starter-webfluxspring-doc.cadn.net.cn

HTTP、WebSocketspring-doc.cadn.net.cn

Spring WebFluxspring-doc.cadn.net.cn

spring-boot-starter-rsocketspring-doc.cadn.net.cn

TCP、WebSocketspring-doc.cadn.net.cn

Reactor Netty 上的 Spring WebFluxspring-doc.cadn.net.cn

GraphQL 架构

Spring GraphQL 应用程序在启动时需要定义的架构。 默认情况下,您可以在src/main/resources/graphql/**Spring Boot 将自动选取它们。 您可以使用spring.graphql.schema.locations和带有spring.graphql.schema.file-extensions.spring-doc.cadn.net.cn

如果您希望 Spring Boot 检测该位置的所有应用程序模块和依赖项中的 schema 文件, 您可以设置spring.graphql.schema.locations"classpath*:graphql/**/"(请注意classpath*:前缀)。

在以下部分中,我们将考虑此示例 GraphQL 架构,定义两种类型和两种查询:spring-doc.cadn.net.cn

type Query {
    greeting(name: String! = "Spring"): String!
    project(slug: ID!): Project
}

""" A Project in the Spring portfolio """
type Project {
    """ Unique string id used in URLs """
    slug: ID!
    """ Project name """
    name: String!
    """ URL of the git repository """
    repositoryUrl: String!
    """ Current support status """
    status: ProjectStatus!
}

enum ProjectStatus {
    """ Actively supported by the Spring team """
    ACTIVE
    """ Supported by the community """
    COMMUNITY
    """ Prototype, not officially supported yet  """
    INCUBATING
    """ Project being retired, in maintenance mode """
    ATTIC
    """ End-Of-Lifed """
    EOL
}
默认情况下,架构上将允许现场自省,因为 GraphiQL 等工具需要它。 如果您不想公开有关架构的信息,可以通过设置spring.graphql.schema.introspection.enabledfalse.

GraphQL 运行时布线

The GraphQL JavaRuntimeWiring.Builder可用于注册自定义标量类型、指令、类型解析器、DataFetcher等。 您可以声明RuntimeWiringConfigurerbeans 来访问RuntimeWiring.Builder. Spring Boot 会检测此类 bean 并将它们添加到 GraphQlSource 构建器中。spring-doc.cadn.net.cn

但是,应用程序通常不会实现DataFetcher直接创建带注释的控制器。 Spring Boot 将自动检测@Controller类,并将其注册为DataFetchers. 以下是我们的问候查询的示例实现,其中@Controller类:spring-doc.cadn.net.cn

import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.stereotype.Controller;

@Controller
public class GreetingController {

	@QueryMapping
	public String greeting(@Argument String name) {
		return "Hello, " + name + "!";
	}

}
import org.springframework.graphql.data.method.annotation.Argument
import org.springframework.graphql.data.method.annotation.QueryMapping
import org.springframework.stereotype.Controller

@Controller
class GreetingController {

	@QueryMapping
	fun greeting(@Argument name: String): String {
		return "Hello, $name!"
	}

}

Querydsl 和 QueryByExample 存储库支持

运输

HTTP 和 WebSocket

GraphQL HTTP 终端节点位于 HTTP POST/graphql默认情况下。 它还支持"text/event-stream"media type over Server Sent Events 仅适用于订阅。 路径可以使用spring.graphql.path.spring-doc.cadn.net.cn

Spring MVC 和 Spring WebFlux 的 HTTP 端点都由RouterFunction带有@Order0. 如果您定义自己的RouterFunctionbeans 中,您可能希望添加适当的@Order注解,以确保它们被正确排序。

默认情况下,GraphQL WebSocket 终端节点处于关闭状态。要启用它:spring-doc.cadn.net.cn

Spring GraphQL 提供了一个 Web 拦截模型。 这对于从 HTTP 请求标头中检索信息并在 GraphQL 上下文中设置信息或从同一上下文获取信息并将其写入响应标头非常有用。 使用 Spring Boot,您可以声明WebGraphQlInterceptorbean 将其注册到 Web 传输中。spring-doc.cadn.net.cn

Spring MVCSpring WebFlux 支持 CORS(跨域资源共享)请求。 CORS 是 GraphQL 应用程序的 Web 配置的关键部分,这些应用程序可以从使用不同域的浏览器访问。spring-doc.cadn.net.cn

Spring Boot 在spring.graphql.cors.*Namespace;下面是一个简短的配置示例:spring-doc.cadn.net.cn

spring.graphql.cors.allowed-origins=https://example.org
spring.graphql.cors.allowed-methods=GET,POST
spring.graphql.cors.max-age=1800s
spring:
  graphql:
    cors:
      allowed-origins: "https://example.org"
      allowed-methods: GET,POST
      max-age: 1800s

RSocket 系列

还支持将 RSocket 作为 WebSocket 或 TCP 之上的传输。 配置 RSocket 服务器后,我们可以使用spring.graphql.rsocket.mapping. 例如,将该映射配置为"graphql"意味着我们可以在使用RSocketGraphQlClient.spring-doc.cadn.net.cn

Spring Boot 会自动配置RSocketGraphQlClient.Builder<?>bean 中可以注入到组件中:spring-doc.cadn.net.cn

@Component
public class RSocketGraphQlClientExample {

	private final RSocketGraphQlClient graphQlClient;

	public RSocketGraphQlClientExample(RSocketGraphQlClient.Builder<?> builder) {
		this.graphQlClient = builder.tcp("example.spring.io", 8181).route("graphql").build();
	}
@Component
class RSocketGraphQlClientExample(private val builder: RSocketGraphQlClient.Builder<*>) {

然后发送请求: include-code::RSocketGraphQlClientExample[tag=request]spring-doc.cadn.net.cn

异常处理

Spring GraphQL 使应用程序能够注册一个或多个 SpringDataFetcherExceptionResolver按顺序调用的组件。 异常必须解析为GraphQLError对象,请参阅 Spring GraphQL 异常处理文档。 Spring Boot 将自动检测DataFetcherExceptionResolverbean 并将它们注册到GraphQlSource.Builder.spring-doc.cadn.net.cn

GraphiQL 和 Schema 打印机

Spring GraphQL 提供了基础设施,可帮助开发人员使用或开发 GraphQL API。spring-doc.cadn.net.cn

Spring GraphQL 附带一个默认的 GraphiQL 页面,该页面在"/graphiql"默认情况下。 默认情况下,此页面处于禁用状态,可以使用spring.graphql.graphiql.enabled财产。 许多公开此类页面的应用程序将首选自定义版本。 默认实现在开发过程中非常有用,这就是为什么它会自动公开spring-boot-devtools在开发过程中。spring-doc.cadn.net.cn

您还可以选择以文本格式公开 GraphQL 架构,网址为/graphql/schemaspring.graphql.schema.printer.enabled属性已启用。spring-doc.cadn.net.cn