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());