You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

同应用内能否使用GraphQL客户端调用内部GraphQL服务类?

解决方案

SmallRye GraphQL 本身内置了内存级执行引擎,完全不需要通过HTTP客户端调用本地服务,也不需要引入额外的客户端组件,直接复用CDI容器中已经注册好的核心执行器即可实现无网络请求的GraphQL查询处理。

实现逻辑说明

  • 你之前尝试使用的DynamicGraphQLClientBuilder属于SmallRye GraphQL的远程客户端组件,设计目标是调用外部GraphQL服务,底层默认走HTTP协议,不适合本地同应用调用场景。
  • Quarkus SmallRye GraphQL扩展在启动时会自动扫描所有标注@GraphQLApi的类(包括你写的FilmResource),将其中定义的查询、变更操作注册到内置的GraphQL核心执行Bean中,这个Bean可以直接注入使用,所有查询执行全程在内存中完成,没有任何网络开销。
  • 该执行引擎的字段裁剪、参数绑定、异常处理逻辑和Quarkus默认暴露的/graphql HTTP端点完全一致,你不需要对已有的FilmResource代码做任何修改。

可直接复用的代码实现

首先确保项目已经引入Quarkus SmallRye GraphQL和AWS Lambda扩展依赖,不需要额外引入GraphQL客户端依赖。
在Lambda的RequestHandler实现类中直接注入核心执行器:

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import graphql.ExecutionInput;
import io.smallrye.graphql.api.GraphQL;
import io.vertx.core.json.JsonObject;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import java.util.Collections;

@Named("filmGraphqlHandler")
public class FilmGraphqlHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {

    @Inject
    GraphQL graphQLEngine;

    @Override
    public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent event, Context context) {
        try {
            JsonObject requestPayload = new JsonObject(event.getBody());
            String query = requestPayload.getString("query");
            JsonObject variables = requestPayload.getJsonObject("variables");
            String operationName = requestPayload.getString("operationName");

            // 内存直接执行查询,无HTTP调用
            var executionResult = graphQLEngine.execute(
                    ExecutionInput.newExecutionInput()
                            .query(query)
                            .variables(variables == null ? Collections.emptyMap() : variables.getMap())
                            .operationName(operationName)
                            .build()
            );

            JsonObject responseBody = new JsonObject()
                    .put("data", executionResult.getData())
                    .put("errors", executionResult.getErrors());

            return new APIGatewayProxyResponseEvent()
                    .withStatusCode(200)
                    .withHeaders(Collections.singletonMap("Content-Type", "application/json"))
                    .withBody(responseBody.encode());
        } catch (Exception e) {
            return new APIGatewayProxyResponseEvent()
                    .withStatusCode(500)
                    .withBody(new JsonObject().put("message", "Internal server error").encode());
        }
    }
}

部署注意事项

  • 如果采用GraalVM原生镜像部署Lambda,Quarkus SmallRye GraphQL扩展会自动生成所需的反射、资源配置,不需要手动调整原生镜像构建参数。
  • 该实现的性能远高于本地HTTP回环调用的方案,冷启动和热执行延迟都更低,非常适合Lambda这种轻量运行场景。
  • 客户端传入的查询格式和标准GraphQL HTTP请求完全一致,比如你提到的只查询allFilms下title、releaseDate字段的请求可以直接使用,不需要做格式转换。

内容的提问来源于stack exchange,提问作者Konstantin Rezchikov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.27 16:31:36