Consuming graphql query in Java

1k Views Asked by At

I have Graphql query in this format :

query {
   priceData {
    customer 
    id
    title
    dob
  }
}

In java, we use this query in String format Like :

"{\"query\":\"query {\\r\\n   priceData {\\r\\n    customer \\r\\n    id\\r\\n    title\\r\\n    dob\\r\\n  }\\r\\n}\",\"variables\":{}}"

Here we hardcoded this query. Is there any other where we generate this query dynamically? Something like I create a POJO java class or Map type and later parse it to convert in GraphQL query string.

2

There are 2 best solutions below

0
On

You can use apollo graphql client. You'll have to store the schema and query in json files and use pojos and mappers to create the complete request dynamically.

Docs: https://www.apollographql.com/docs/android/essentials/get-started-java/

0
On

I am not really sure if there is any library that can help you with this, but I don't think so (only for the creation of the schema but that is a concern for the server not for you as the client). What you can actually do is store the String in a file and read it and store it in memory when your Java application starts. This way it would be easier to read and simpler to change if needed.