GraphQL 支援

Spring Integration 提供通道配接器,用於與 GraphQL 通訊協定互動。此實作基於 Spring for GraphQL

您需要將此相依性包含到您的專案中

  • Maven

  • Gradle

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

GraphQL 輸出閘道器

GraphQlMessageHandlerAbstractReplyProducingMessageHandler 擴充功能,代表執行 GraphQL querymutationsubscription 作業並產生其結果的輸出閘道器契約。它需要 org.springframework.graphql.ExecutionGraphQlService 來執行 operation,這可以靜態設定,也可以透過 SpEL 運算式針對請求訊息進行設定。operationName 是選用的,也可以靜態設定,或透過 SpEL 運算式進行設定。variablesExpression 也是選用的,用於參數化作業。locale 是選用的,用於 GraphQL Java 程式庫中的作業執行內容。executionId 可以透過 SpEL 運算式設定,預設為請求訊息的 id 標頭。

如果請求訊息的酬載是 ExecutionGraphQlRequest 的執行個體,則 GraphQlMessageHandler 中不會執行任何設定動作,並且此類輸入會按原樣用於 ExecutionGraphQlService.execute()。否則,將根據請求訊息使用上述 SpEL 運算式判斷 operationoperationNamevariablesexecutionId

GraphQlMessageHandler 是反應式串流元件,並產生 Mono<ExecutionGraphQlResponse> 回覆,作為 ExecutionGraphQlService.execute(ExecutionGraphQlRequest) 的結果。此類 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,必須手動訂閱和處理。或者,可以透過純服務啟動器將其平面對應到 FluxMessageChannel 的回覆。

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

此類輸出閘道器不僅可用於透過 HTTP 的 GraphQL 請求,也可用於任何上游端點,這些端點在訊息中產生或攜帶 GraphQL 作業或其引數。GraphQlMessageHandler 處理的結果可以作為對上游請求的回覆產生,或向下游傳送以在整合流程中進一步處理。