java - How to get json from GraphQL
问题描述
Hi I'm making a bridge that translates GraphQL's otherwise query to another service. I had the idea to take json from a query to GraphQL and make the query via resttemplate. But I couldn't find a way to take the request body. I will be glad to see an example of your code or another more competent solution.
STACK: 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);
}
}
解决方案
创建一个JsonObject实例。将您的查询添加到属性。将变量添加到同一个对象。
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("query", query);
jsonObject.add("variables", variables);
在您的 java 代码中调用查询,如下所示:
HttpResponse<String> httpResponse = post(jsonObject.toString());
推荐阅读
- javascript - 是否可以创建相同 html 元素的无限循环,从顶部滚动到底部并具有类似门户的效果?
- c++ - 如何以内存泄露的形式创建信息泄漏?(源代码)
- pip - simpletraj(setup.py)的构建轮...错误
- javascript - 选中的复选框在第一时间不起作用
- android - OnClickListener 仅适用于第一个 ViewHolder
- python - 具有均匀间隔颜色的颜色条的不均匀(不规则)间隔数据
- linux - 您是否必须添加访客才能启用共享剪贴板?
- javascript - 如何在 Safari 13 Web Audio API 上获取 AudioListener.positionX
- php - PHP ZipArchive 600 文件权限
- node.js - 当用户在输入类型文件中选择时如何获取文件的路径目录