3

I need to do some pre-parsing steps before executing a Graphql query. But I have no idea how to convert an input Graphql query, which is a String, into a Java Object. Ideally, the Java object should contain the subfields and the arguments of each field of the query. For example:

{
  Instrument(id: "1234") {
    Reference {
      Name
    }
  }
}

This may be converted into something like a JSON object:

{
  "Instrument":{
    "arguments:{
      "id": "1234"
    }
    "fields":{
      "Reference":{
        "fields":{
          "Name": "String"
        }
      }
    }
  }
}

Any help is highly appreciated :)

2
  • Does this answer your question? How to Parse & Marshal GraphQL Response to Java POJOs? Commented Mar 25, 2022 at 11:52
  • @pringi Thanks for the answer. However, it doesn't quite answer my question. This article tries to define the data fetcher for each field and execute the query, and then parse the result. I thought about this before, but I would like to have a way without set the behavior of data fetcher. Finally I found a solution. I'll put to the answer in a minute. Commented Apr 1, 2022 at 3:08

1 Answer 1

4

Below code just parse the GraphQL query grammar, even without knowing the schema.

import graphql.language.Document;
import graphql.parser.Parser;
Parser parser = new Parser(); 
Document document = parser.parseDocument("{  Instrument(id: \"1234\") {    Reference {      Name    }  }}");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.