Convert a comma separated string of fields to a GraphQL query syntax

Convert a comma separated string of fields to a GraphQL query syntax


-1

I have a requiremenet where I need to parse a user input comma separated string, and generate a graphql query out of it.

INPUT:

queryPath
fieldA.fieldB, fieldA.fieldC, fieldA.fieldD.fieldE, fieldA.fieldD.fieldF, fieldA.fieldD.fieldG

OUTPUT:

query{
    queryPath{
        fieldA{
            fieldB
            fieldC
            fieldD{
                fieldE
                fieldF
                fieldG
            }
        }
    }
}

I’m using Java to do this, thanks in advance.

EDIT:
So far I’ve gotten the following output but unsure how to reach the expected output.

[fieldA, fieldB]
[fieldA, fieldC]
[fieldA, fieldD, fieldE]
[fieldA, fieldD, fieldF]
[fieldA, fieldD, fieldG]

Code:

Arrays.stream(stringInput.split(","))
    .map(s -> Arrays.stream(s.split("\."))
        .map(String::trim)
        .collect(Collectors.toList()))
    .collect(Collectors.toList());


Load 6 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *