1

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


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

This title and content for this question was made by "Ahmad Shaikh" at this link: https://stackoverflow.com/questions/77569390/convert-a-comma-separated-string-of-fields-to-a-graphql-query-syntax. Contributions on stackoverflow.com are made under this license.

Java Linux Web

0 Answers

Give your answer

Be sure to answer with an explanation on why your solution works.

Don't see an answer for your question?
Ask here

Search tags used in this post