对于最新的稳定版本,请使用 Spring Integration 6.3.1Spring中文文档

对于最新的稳定版本,请使用 Spring Integration 6.3.1Spring中文文档

Spring Integration 提供了用于与 GraphQL 协议交互的通道适配器。 该实现基于 Spring for GraphQLSpring中文文档

您需要将此依赖项包含在项目中:Spring中文文档

<dependency>
    <groupId>org.springframework.integration</groupId>
    <artifactId>spring-integration-graphql</artifactId>
    <version>6.2.6</version>
</dependency>
compile "org.springframework.integration:spring-integration-graphql:6.2.6"

GraphQL 出站网关

这是一个扩展,表示一个出站网关协定,用于执行 GraphQL 或操作并生成其结果。 它需要 a 来执行 ,可以静态配置,也可以通过针对请求消息的 SpEL 表达式进行配置。 这是可选的,也可以静态配置或通过 SpEL 表达式配置。 这也是可选的,用于参数化操作。 是可选的,用于 GraphQL Java 库中的操作执行上下文。 可以通过 SpEL 表达式进行配置,并默认为请求消息的标头。GraphQlMessageHandlerAbstractReplyProducingMessageHandlerquerymutationsubscriptionorg.springframework.graphql.ExecutionGraphQlServiceoperationoperationNamevariablesExpressionlocaleexecutionIdidSpring中文文档

如果请求消息的有效负载是 的实例,则不会在 中执行任何设置操作,并且此类输入将按原样用于 。 否则,将使用上述 SpEL 表达式根据请求消息确定 、 和 。ExecutionGraphQlRequestGraphQlMessageHandlerExecutionGraphQlService.execute()operationoperationNamevariablesexecutionIdSpring中文文档

是一个反应式流组件,并生成一个回复,作为 . 当输出通道不是反应性的时,框架在输出通道中或异步中订阅此类 a。 有关如何处理 GraphQL 操作结果的文档,请参阅文档。GraphQlMessageHandlerMono<ExecutionGraphQlResponse>ExecutionGraphQlService.execute(ExecutionGraphQlRequest)MonoReactiveStreamsSubscribableChannelAbstractMessageProducingHandlerExecutionGraphQlResponseSpring中文文档

@Bean
GraphQlMessageHandlerSpec graphQlMessageHandlerSpec(ExecutionGraphQlService graphQlService) {
    return GraphQl.gateway(graphQlService)
            .operation("""
                    query HeroNameAndFriends($episode: Episode) {
                      hero(episode: $episode) {
                        name
                        friends {
                          name
                        }
                      }
                    }""")
            .variablesExpression("{episode:'JEDI'}");
}

@Bean
IntegrationFlow graphqlQueryMessageHandlerFlow(GraphQlMessageHandler handler) {
    return IntegrationFlow.from(MessageChannels.flux("inputChannel"))
            .handle(handler)
            .channel(c -> c.flux("resultChannel"))
            .get();
}

@Bean
ExecutionGraphQlService graphQlService(GraphQlSource graphQlSource) {
    return new DefaultExecutionGraphQlService(graphQlSource);
}

@Bean
GraphQlSource graphQlSource(AnnotatedControllerConfigurer annotatedDataFetcherConfigurer) {
    return GraphQlSource.builder()
            .schemaResources(new ClassPathResource("graphql/test-schema.graphqls"))
            .configureRuntimeWiring(annotatedDataFetcherConfigurer)
            .build();
}

@Bean
AnnotatedControllerConfigurer annotatedDataFetcherConfigurer() {
    return new AnnotatedControllerConfigurer();
}

应对订阅操作的结果进行特殊处理。 在这种情况下,返回必须手动订阅和处理的返回。 或者,它可以通过普通服务激活器平面映射到以下回复:ExecutionGraphQlResponse.getData()SubscriptionPublisherFluxMessageChannelSpring中文文档

@ServiceActivator(inputChannel = "graphQlResultChannel", outputChannel="graphQlSubscriptionChannel")
public SubscriptionPublisher obtainSubscriptionResult(ExecutionGraphQlResponse graphQlResponse) {
	return graphQlResponse.getData();
}

这种出站网关不仅可以通过 HTTP 用于 GraphQL 请求,还可以用于从任何生成或携带消息中的 GraphQL 操作或其参数的上游端点。 处理结果可以作为对上游请求的回复生成,也可以发送到下游,以便在集成流中进一步处理。GraphQlMessageHandlerSpring中文文档