我们正在使用带有 DGS Netflix graphql 的 Spring Boot 开发一个项目。我们创建了所有模式和数据,它们在默认端点“/graphql”下工作得非常好。我们想用自定义端点公开这个应用程序,所以我们试图添加一个带有自定义端点的控制器,如下所示。但是当我运行应用程序并发送查询时,我的数据获取器被调用了两次。第一次从控制器调用,第二次我相信它来自框架本身。有人对此有任何想法,为什么它被调用两次以及如何避免它?非常感谢您的帮助。请参阅下面的 Datafetcher 和 Controller。
Controller:
@RestController
@RequestMapping("/sample-information/model")
@Slf4j
public class CustomController {
@Autowired
DgsQueryExecutor dgsQueryExecutor;
@PostMapping(consumes = {MediaType.APPLICATION_JSON_VALUE, "application/graphql"})
public Mono<ResponseEntity<Object>> getDetails(@RequestBody String query,
@RequestHeader HttpHeaders headers
) {
GraphQLQueryInput inputs = null;
try {
inputs = ObjectMapperHelper.objectMapper.readValue(query, GraphQLQueryInput.class);
} catch (Exception e) {
log.error("TraceId: {} - Application Error: Error message: Error converting query to GraphQLQueryInput: {} "+ query);
}
if(inputs.getVariables() == null) {
inputs.setVariables(new HashMap<>());
}
if(inputs.getOperationName() == null) {
inputs.setOperationName("");
}
final String que = inputs.getQuery();
final Map<String, Object> var = inputs.getVariables();
final String opn = inputs.getOperationName();
ExecutionInput.Builder executionInput = ExecutionInput.newExecutionInput()
.query(inputs.getQuery())
.operationName(inputs.getOperationName())
.variables(inputs.getVariables());
return Mono.fromCallable(()-> {
return dgsQueryExecutor.execute(que, var, opn);
}).subscribeOn(Schedulers.elastic()).map(result -> {
return new ResponseEntity<>(result, HttpStatus.OK);
});
}
}
Datafetcher:
@DgsComponent
@Slf4j
public class SampleDataFetcher {
@Autowired
SampleBuilder sampleBuilder;
@DgsData(parentType = DgsConstants.QUERY_TYPE, field = DgsConstants.QUERY.SampleField)
public CompletableFuture<StoreDirectoryByStateResponse> getStoreDirectoryByState(@InputArgument String state, DataFetchingEnvironment dfe) throws BadRequestException {
Mono<StoreDirectoryByStateResponse> storeDirectoryResponse = null;
try {
sampleResponse = sampleBuilder.buildResponse(modelGraphQLContext);
} catch (BaseException e) {
}
return sampleResponse.map(response -> {
return response;
}).toFuture();
}
}