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.