Reputation: 2820
I'm using GraphQL JVM Client by American Express
This is the query I like to build:
exercise {
id
name
images(resize: {width: 512, height: 288, background: "ffffff"})
}
This is the DTO I have created:
@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() {
}
}
This is, how I want to build the query:
GraphQLTemplate graphQLTemplate = new GraphQLTemplate();
GraphQLRequestEntity requestEntity = GraphQLRequestEntity.Builder()
.url("https://domain/graphql")
.arguments(
new Arguments("exercise.images", new Argument("resize", ?))
)
.request(Exercise.class)
.build();
But I do not get the correct expression for ?.
The question is, how do I pass structured parameters as argument?
Upvotes: 1
Views: 2378
Reputation: 176
The solution to this is using the InputObject class. Your DTO can remain exactly as it is defined, simply add the argument like this:
GraphQLTemplate graphQLTemplate = new GraphQLTemplate();
InputObject resizeInput = new InputObject.Builder()
.put("width", 512)
.put("height", 288)
.put("background", "ffffff")
.build();
GraphQLRequestEntity requestEntity = GraphQLRequestEntity.Builder()
.url("https://domain/graphql")
.arguments(
new Arguments("exercise.images", new Argument("resize", resizeInput))
)
.request(Exercise.class)
.build();
You can read more about it and some of the other API uses in this post too (https://americanexpress.io/graphql-for-the-jvm/)
Hope this helps!
Upvotes: 3
Reputation: 2820
Ok, I found a solution working for me...
I have created a "variable" class
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+"\"}";
}
}
and the missing thing was overriding toString() method. Then, this is possible:
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();
which results in the correct query.
Upvotes: 0