americanexpress / nodes

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

Is ther a way to set a list of InputObjects as an argument using the client ? #34

Closed bpillai closed 6 years ago

bpillai commented 6 years ago

example

xyz(abc:[{def:enum, value:"string"},{def:enum, value:"string"}]) { }

chemdrew commented 6 years ago

Hi @bpillai, yes.

        List<String> stringList = new ArrayList<String>();
        stringList.add("string1");
        stringList.add("string2");
        stringList.add("string3");
        InputObject input = new InputObject.Builder<Object>()
                .put("input1", 1)
                .put("input2", true)
                .put("input3", stringList)
                .put("input4", "string")
                .build();
        List<InputObject> inputs = new ArrayList<InputObject>();
        inputs.add(input);
        inputs.add(input);
        GraphQLRequestEntity requestEntity = GraphQLRequestEntity.Builder()
                .url(EXAMPLE_URL)
                .arguments(new Arguments("test", new Argument("id", inputs)))
                .request(TestModel.class)
                .build();

returns with

query {
  test (id:[InputObject{map={input4=string, input3=[string1, string2, string3], input2=true, input1=1}},InputObject{map={input4=string, input3=[string1, string2, string3], input2=true, input1=1}}]) {
      ...
chemdrew commented 6 years ago

see this: https://github.com/americanexpress/nodes/pull/35/commits/e6669c77a2a5935e5b22829e0eb03c7135670303#diff-9e6159458cd498bc29877d5cbfc80b13

bpillai commented 6 years ago

hi @chemdrew : this creates an invalid syntax . I was looking for query { test (id:[{input4=string, input3=[string1, string2, string3], input2=true, input1=1},{input4=string, input3=[string1, string2, string3], input2=true, input1=1}}]) {} instead of query { test (id:[InputObject{map={input4=string, input3=[string1, string2, string3], input2=true, input1=1}},InputObject{map={input4=string, input3=[string1, string2, string3], input2=true, input1=1}}]) {

bpillai commented 6 years ago

rather than updating the toString adding the following in the StringUtils.java formatGraphQLArgumentList seems to solve the issue .

private static String formatGraphQLArgumentList(List values) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("["); Pattern p = Pattern.compile("^\$"); for (Object value: values) { if (stringBuilder.length() != 1) stringBuilder.append(","); Matcher m = p.matcher("" + value); System.out.println(value.getClass()); if (value instanceof String && !"null".equalsIgnoreCase((String) value) && !m.find()) { stringBuilder.append("\"").append(value).append("\""); } else if (value instanceof InputObject) { stringBuilder.append(((InputObject) value).getMessage()); } else { stringBuilder.append(value); } } stringBuilder.append("]"); return stringBuilder.toString(); }

chemdrew commented 6 years ago

:facepalm: great catch! Would you like to open the pull request fixing my mistake as you described there for some credit as a contributor?

bpillai commented 6 years ago

I am not able to push the branch to your repository git push -u origin feature/add-list-of-input-objects-in-arguments git@github.com: Permission denied (publickey). fatal: Could not read from remote repository.

Please make sure you have the correct access rights and the repository exists.

chemdrew commented 6 years ago

I’ll be home to look into and help correct it in about an hour or so

chemdrew commented 6 years ago

Oh, just create a fork of this repo. Commit to your fork, then raise the pull request from your fork

bpillai commented 6 years ago

PR : https://github.com/americanexpress/nodes/pull/36 I have signed the CLA . But is marked as pending . If the code changes are available master soon that would be great . Thank you .

chemdrew commented 6 years ago

Awesome, sometimes it takes a few minutes to update. Will just check on that periodically

chemdrew commented 6 years ago

Thank you for your first contribution on Nodes! Congrats!