【问题标题】:How to get json from GraphQL如何从 GraphQL 获取 json
【发布时间】:2021-06-27 08:42:04
【问题描述】:

您好,我正在搭建一个桥梁,将 GraphQL 的其他查询转换为另一个服务。我的想法是从查询中获取 json 到 GraphQL 并通过 resttemplate 进行查询。但我找不到获取请求正文的方法。 我很高兴看到您的代码示例或其他更有效的解决方案。

堆栈:Spring boot+ java 8 + graphql

@Component
public class VehicleQuery implements GraphQLQueryResolver {

    @Autowired
    private VehicleService vehicleService;

    public List<Vehicle> getVehicles(final int count) {
        return this.vehicleService.getAllVehicles(count);
    }

    public Optional<Vehicle> getVehicle(final int id) {
        return this.vehicleService.getVehicle(id);
    }
}

【问题讨论】:

    标签: java spring spring-boot graphql


    【解决方案1】:

    创建一个 JsonObject 实例。将您的查询添加到属性。将变量添加到同一个对象。

    JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("query", query);
    jsonObject.add("variables", variables);
    

    像这样在你的 java 代码中调用查询:

    HttpResponse<String> httpResponse = post(jsonObject.toString());
    

    【讨论】:

    • 我无法从请求中获取查询(
    • 你有什么输入?你能把它放在这里或举个例子吗?我不确定你的意思
    • 我有 Springboot + graphql,这会创建虚拟控制器,控制器将其放入查询解析器中。但我不知道如何获得查询。我用我的 queryResolver 更新描述
    • 您不能创建查看 Vehicle POJO 的查询吗?您需要查询才能获得响应
    • 您可以使用DataFetchingEnvironment获取查询和变量。
    猜你喜欢
    • 2021-12-26
    • 2021-11-05
    • 2018-08-21
    • 2018-02-25
    • 1970-01-01
    • 2019-12-02
    • 2021-07-27
    • 2019-07-30
    • 2020-09-11
    相关资源
    最近更新 更多