americanexpress / nodes

A GraphQL JVM Client - Java, Kotlin, Scala, etc.
Apache License 2.0
307 stars 70 forks source link

Array of Objects/InputObjects inside of an InputObject #45

Closed albertonaperijr closed 5 years ago

albertonaperijr commented 5 years ago

E.g.

{
  currency: "USD",
  totalAmount: 10.00,
  items: [{
    name: "test1",
    currency: "USD",
    price: 5.00
  }, {
    name: "test2",
    currency: "USD",
    price: 5.00
  }]
}

I've tried it with the code below but items are not generating properly.

InputObject item1 = new InputObject.Builder<>()
        .put("name", "test1")
        .put("currency", "USD")
        .put("price", 5.00)
        .build();
InputObject item2 = new InputObject.Builder<>()
        .put("name", "test2")
        .put("currency", "USD")
        .put("price", 5.00)
        .build();

List<InputObject> items = new ArrayList<>();
items.add(item1);
items.add(item2);

InputObject input = new InputObject.Builder<>()
        .put("currency", "USD")
        .put("totalAmount", 10.00)
        .put("items", items)
        .build();

Actual Output:

mutation { createPayment (input:{currency:"USD",totalAmount:10.00,items:[io.aexp.nodes.graphql.InputObject@74a0c8e4,io.aexp.nodes.graphql.InputObject@82a0c0a5]}) { paymentId } }

Expected Output:

mutation { createPayment (input:{currency:"USD",totalAmount:10.00,items:[{name:"test1",currency:"USD",price:5.00},{name:"test2",currency:"USD",price:5.00}]}) { paymentId } }

Thanks.

chemdrew commented 5 years ago

Hi @albertonaperijr,

This was a bug from v0.1.0-v0.1.2 and was fixed in v0.1.3. Please upgrade to the latest v0.3.0.

Original issue here #34

albertonaperijr commented 5 years ago

Hi @chemdrew,

I'm actually encountering this issue using the latest version (v0.3.0). I'm not sure if the issue #34 was actually related to this case since it's using an array of InputObjects directly as an argument while in this case, I need the array of InputObjects inside of an InputObject.

Thanks.

chemdrew commented 5 years ago

Payment.java

@GraphQLProperty(name = "createPayment", arguments = {
        @GraphQLArgument(name = "input")
})
public class Payment {
    private String paymentId;

    public String getPaymentId() {
        return paymentId;
    }

    public void setPaymentId(String paymentId) {
        this.paymentId = paymentId;
    }

    @Override
    public String toString() {
        return "Payment{" +
                "paymentId='" + paymentId + '\'' +
                '}';
    }
}

Main.java

public class Main {
    public static void main(String[] args) {
        InputObject item1 = new InputObject.Builder()
                .put("name", "test1")
                .put("currency", "USD")
                .put("price", 5.00)
                .build();
        InputObject item2 = new InputObject.Builder()
                .put("name", "test2")
                .put("currency", "USD")
                .put("price", 5.00)
                .build();

        List<InputObject> items = new ArrayList<InputObject>();
        items.add(item1);
        items.add(item2);

        InputObject input = new InputObject.Builder()
                .put("currency", "USD")
                .put("totalAmount", 10.00)
                .put("items", items)
                .build();

        final GraphQLRequestEntity requestEntity = GraphQLRequestEntity.Builder()
                .url("https://graphql.example.com")
                .request(Payment.class)
                .arguments(new Arguments("createPayment", new Argument("input", input)))
                .build();
        System.out.println(requestEntity.getRequest());
    }
}

prints out

query { createPayment (input:{totalAmount:10.0,currency:"USD",items:[{price:5.0,name:"test1",currency:"USD"},{price:5.0,name:"test2",currency:"USD"}]}) { paymentId } }

Can you post more of your code to see if it matches up?

albertonaperijr commented 5 years ago

Hi @chemdrew,

I've created a sample project and tried to run the code and it's working just fine. I'm not sure why it's not working on one of my existing projects.

Anyway, thanks for taking the time to solve this issue.

chemdrew commented 5 years ago

Yeah, for sure, no problem. Let me know if the issue persists or you recreate it in the latest version though, wanna make sure there aren’t any bugs hiding.

albertonaperijr commented 5 years ago

It's working now. I think the issue was with my IDE (IntelliJ). I'd updated the dependency version in my pom.xml but it didn't automatically update the local dependencies when clicking the run button. I've fixed it by manually running mvn idea:idea and mvn clean install in the command line.