Below is a typical graphql endpoint for a java backend
There are 2 basic flows here
1 an endpoint for http request that can handle the graghql query as a string and a map / json representation of the query's input variables
2 the graphql wiring for the backend that collates and returns the data
the backend would typicaly have an endpoint that looks like this (1)
public Map<String, Object> graphqlGET(@RequestParam("query") String query,
@RequestParam(value = "operationName", required = false) String operationName,
@RequestParam("variables") String variablesJson) throws IOException {...
note we have 3 inputs
a query string,
a string usually json for the querys variables
an optional "operationName"
once we have parsed these input parameters we would typically send them to the graphql implementation for the query
which could look like this (1)
private Map<String, Object> executeGraphqlQuery(String operationName,
String query, Map<String, Object> variables) {
ExecutionInput executionInput = ExecutionInput.newExecutionInput()
.query(query)
.variables(variables)
.operationName(operationName)
.build();
return graphql.execute(executionInput).toSpecification();
}
here the graphql object has all the wiring to return the data
So a solution is just to post the correctly formatted input parameters to the backend
I often use android and a http client that works with older android versions so a post request in kotlin could look like this as a very simple example
val client = HttpClients.createDefault()
val httpPost = HttpPost(url)
val postParameters = ArrayList<NameValuePair>()
postParameters.add(BasicNameValuePair("query", "query as string"))
postParameters.add(BasicNameValuePair("variables", "variables json string"))
httpPost.entity = UrlEncodedFormEntity(postParameters, Charset.defaultCharset())
val response = client.execute(httpPost)
val ret = EntityUtils.toString(response.getEntity())
please note the implementation for the http post with be dependent on the way the backend java implementation is set up
for the basic http client and post setup many good examples here
How to use parameters with HttpPost
possibly related
graphql allows for a introspection flow which publishes details on the query structure the implementation supports
more info here
https://graphql.org/learn/introspection/
[1] https://github.com/graphql-java/graphql-java-examples