1

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?

2 Answers 2

3

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!

Sign up to request clarification or add additional context in comments.

1 Comment

Your solution fits better as it works with API internals. Thanks!
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.