Java 使用americanexpress/nodes的带有对象的GraphQL查询
我正在使用 这是我想要构建的查询:Java 使用americanexpress/nodes的带有对象的GraphQL查询,java,graphql,Java,Graphql,我正在使用 这是我想要构建的查询: exercise { id name images(resize: {width: 512, height: 288, background: "ffffff"}) } 这是我创建的DTO: @GraphQLProperty(name = "exercise") public class Exercise { private Integer id; private String name; @GraphQLProperty(na
exercise {
id
name
images(resize: {width: 512, height: 288, background: "ffffff"})
}
这是我创建的DTO:
@GraphQLProperty(name = "exercise")
public class Exercise {
private Integer id;
private String name;
@GraphQLProperty(name = "images", arguments = {@GraphQLArgument(name = "resize")})
private List<String> images;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public List<String> getImages() {
return images;
}
public void setImages(List<String> images) {
this.images = images;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Exercise() {
}
}
但是我没有得到?的正确表达式
问题是,如何将结构化参数作为参数传递?好的,我找到了一个适合我的解决方案 我创建了一个“变量”类 缺少的是重写toString()方法。那么,这是可能的:
GraphQLTemplate graphQLTemplate = new GraphQLTemplate();
GraphQLRequestEntity requestEntity = GraphQLRequestEntity.Builder()
.url("https://domain/graphql")
.arguments(
new Arguments("exercise.images",
new Argument("resize", new ResizeVariable("ffffff")))
)
.request(Exercise.class)
.build();
这将导致正确的查询。解决方案是使用InputObject类。您的DTO可以完全保持其定义,只需添加如下参数:
GraphQLTemplate GraphQLTemplate=新的GraphQLTemplate();
InputObject resizeInput=新建InputObject.Builder()
.put(“宽度”,512)
.put(“高度”,288)
.put(“背景”、“ffffff”)
.build();
GraphQLRequestEntity requestEntity=GraphQLRequestEntity.Builder()
.url(“https://domain/graphql")
.论点(
新参数(“exercise.images”,新参数(“resize”,resizeInput))
)
.请求(练习.课)
.build();
你可以在这篇文章中阅读更多关于它和其他一些API的用法()
希望这有帮助 您的解决方案更适合使用API内部构件。谢谢
public class ResizeVariable {
private String background;
public ResizeVariable() {
}
public ResizeVariable(String background) {
this.background = background;
}
public String getBackground() {
return background;
}
public void setBackground(String background) {
this.background = background;
}
@Override
public String toString() {
return "{background: \""+background+"\"}";
}
}
GraphQLTemplate graphQLTemplate = new GraphQLTemplate();
GraphQLRequestEntity requestEntity = GraphQLRequestEntity.Builder()
.url("https://domain/graphql")
.arguments(
new Arguments("exercise.images",
new Argument("resize", new ResizeVariable("ffffff")))
)
.request(Exercise.class)
.build();