Fee*_*tak 5 java webclient spring-boot graphql
我在 Spring Boot 中调用 graphQL 突变 API 时陷入困境。让我解释一下我的场景,我有两个微服务,一个是 AuditConsumeService,它使用来自 activeMQ 的消息,另一个是 GraphQL 层,它只是从消费服务获取数据并将其放入数据库中。当我尝试使用 graphql Playground 或 Postman 推送数据时,一切都很好。如何从 AuditConsumeService 推送数据。在 AuditConsumeService 中,我尝试将突变 API 作为字符串发送。负责将其发送到 graphQL 层的方法是
public Mono<String> sendLogsToGraphQL(String logs){
return webClient
.post()
.uri("http://localhost:8080/logs/createEventLog")
.bodyValue(logs)
.retrieve()
.bodyToMono(String.class);
}
Run Code Online (Sandbox Code Playgroud)
注意:我也尝试将数据作为对象传递,但没有用。String logs
将从 activeMQ 提供给它。我发送的数据是;
{
"hasError": false,
"message": "Hello There",
"sender": "Ali Ahmad",
"payload": {
"type": "String",
"title": "Topoic",
"description": "This is the demo description of the activemqq"
},
"serviceInfo":{
"version": "v1",
"date": "2021-05-18T08:44:17.8237608+05:00",
"serverStatus": "UP",
"serviceName": "IdentityService"
}
}
Run Code Online (Sandbox Code Playgroud)
突变会是这样的;
mutation($eventLog:EventLogInput){
createEventLog(eventLog: $eventLog){
hasError
message
payload{
title,
description
}
}
}
Run Code Online (Sandbox Code Playgroud)
其$eventLog
json 主体为;
{
"eventLog": {
"hasError": false,
"message": "Hello There",
"sender": "Ali Ahmad",
"payload": {
"type": "String",
"title": "Topoic",
"description": "This is the demo description of the activemqq"
},
"serviceInfo":{
"version": "v1",
"date": "2021-05-18T08:44:17.8237608+05:00",
"serverStatus": "UP",
"serviceName": "IdentityService"
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑 遵循以下答案,将消费者服务更新为:
@Component
public class Consumer {
@Autowired
private AuditService auditService;
private final String MUTATION_QUERY = "mutation($eventLog: EventLogInput){\n" +
"createEventLog(eventLog: $eventLog){\n" +
"hasError\n" +
"}\n" +
"}";
@JmsListener(destination = "Audit.queue")
public void consumeLogs(String logs) {
Gson gson = new Gson();
Object jsonObject = gson.fromJson(logs, Object.class);
Map<String, Object> graphQlBody = new HashMap<>();
graphQlBody.put("query", MUTATION_QUERY);
graphQlBody.put("variables", "{eventLog: " + jsonObject+ "}");
auditService.sendLogsToGraphQL(graphQlBody);
}
}
Run Code Online (Sandbox Code Playgroud)
现在在`sendLogsToGraphQL'中将变成。
public void sendLogsToGraphQL(Map<String, String> logs) {
log.info("Logs: {} ", logs);
Mono<String> stringMono = webClient
.post()
.uri("http://localhost:8080/graphql")
.bodyValue(BodyInserters.fromValue(logs))
.retrieve()
.bodyToMono(String.class);
log.info("StringMono: {}", stringMono);
return stringMono;
}
Run Code Online (Sandbox Code Playgroud)
数据未发送到具有指定 url 的 graphql 层。
您必须query
在发布请求中将 和 正文作为变量发送,如下所示
graphQlBody = { "query" : mutation_query, "variables" : { "eventLog" : event_log_json } }
Run Code Online (Sandbox Code Playgroud)
然后在webClient中您可以通过多种方式发送正文
public Mono<String> sendLogsToGraphQL(Map<String,Object> body){
return webClient
.post()
.uri("http://localhost:8080/logs/createEventLog")
.bodyValue(BodyInserters.fromValue(body))
.retrieve()
.bodyToMono(String.class);
}
Run Code Online (Sandbox Code Playgroud)
这里我只是展示了如何使用来形成 graphQL 请求主体,但是您也可以使用和Map<String,Object>
的属性创建相应的 POJO 类query
variables