'Consume Grapql mutation API in spring boot using RestTemaplate or WebClient
I got confused while consuming my graphql api in spring boot. the method which is sending data to another service is;
String query = "mutation{\n" +
"createEventLog(eventLog: " + logs + "){\n" +
"hasError\n" +
"}\n" +
"}";
log.info("Query: " + query);
log.info("Query type, {}", query.getClass().getSimpleName());
auditService.sendLogsToGraphQL(query);
while the logs is;
{
"hasError": false,
"message": "hello there",
"sender": "khattak",
"payload":{
"type": "string",
"title": "Event",
"description": "This is the description of the event log"
},
"serviceInfo":{
"version": "v1",
"date": "Thu, 20 Jan 2022 11:24:17 GMT",
"serverStatus": "UP",
"serviceName": "Identity"
}
}
while the sendLogsToGraphQL is;
public void sendLogsToGraphQL(String logs){
try {
restTemplate.postForEntity("http://localhost:8080/graphql",
logs, Boolean.class);
}catch (Exception er){
System.out.println("error = " + er);
}
}
After this when run the application, i get the below output. with 406 error, the full error is;
2022-01-25 17:04:43.703 INFO 19032 --- [ntContainer#0-1] c.v.AuditConsumeService.engine.Consumer : Query: mutation{
createEventLog(eventLog: {
"hasError": false,
"message": "hello there",
"sender": "khattak",
"payload":{
"type": "string",
"title": "Event",
"description": "This is the description of the event log"
},
"serviceInfo":{
"version": "v1",
"date": "Thu, 20 Jan 2022 11:24:17 GMT",
"serverStatus": "UP",
"serviceName": "Identity"
}
}){
hasError
}
}
2022-01-25 17:04:43.703 INFO 19032 --- [ntContainer#0-1] c.v.AuditConsumeService.engine.Consumer : Query type, String
error = org.springframework.web.client.HttpClientErrorException$NotAcceptable: 406 : "{"timestamp":"2022-01-25T12:04:43.786+00:00","status":406,"error":"Not Acceptable","path":"/graphql"}"
In the same way i tried with webclient but useless as;
public Mono<Object> sendLogsToGraphQL(String body) throws IOException {
Mono<Object> block = webClient
.post()
.uri("http://localhost:8080/graphql")//
.bodyValue(BodyInserters.fromValue(body))
.retrieve()
.bodyToMono(Object.class);
System.out.println("Block:: " + block);
return block;
}
I also followed the previous asked question by Feezan khattak, but that's also not working.
Thanks for the Answer in Advance :)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
