GraphQL 支持

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

项目需要此依赖项

  • Maven

  • Gradle

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

GraphQL 出站网关

GraphQlMessageHandlerAbstractReplyProducingMessageHandler 的扩展,表示用于执行 GraphQL querymutationsubscription 操作并生成其结果的出站网关契约。它需要一个 org.springframework.graphql.ExecutionGraphQlService 来执行 operation,可以通过静态配置或通过针对请求消息的 SpEL 表达式进行配置。operationName 是可选的,也可以通过静态配置或通过 SpEL 表达式进行配置。variablesExpression 也是可选的,用于参数化操作。locale 是可选的,用于 GraphQL Java 库中的操作执行上下文。executionId 可以通过 SpEL 表达式配置,默认为请求消息的 id 头。

如果请求消息的载荷是 ExecutionGraphQlRequest 的实例,那么 GraphQlMessageHandler 中不会执行任何设置操作,并且该输入将直接用于 ExecutionGraphQlService.execute()。否则,operationoperationNamevariablesexecutionId 将通过上述 SpEL 表达式针对请求消息确定。

GraphQlMessageHandler 是一个反应式流组件,作为 ExecutionGraphQlService.execute(ExecutionGraphQlRequest) 的结果,它会生成一个 Mono<ExecutionGraphQlResponse> 答复。当输出通道不是反应式时,该 Mono 会由框架在 ReactiveStreamsSubscribableChannel 输出通道中或在 AbstractMessageProducingHandler 中异步订阅。请参阅 ExecutionGraphQlResponse 的文档,了解如何处理 GraphQL 操作结果。

@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() 返回一个 SubscriptionPublisher,必须手动订阅和处理。或者可以通过普通的 service activator 将其扁平映射到 FluxMessageChannel 的回复。

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

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

© . This site is unofficial and not affiliated with VMware.