netflix-graphql中的java控制器

tcomlyy6  于 2021-07-09  发布在  Java
关注(0)|答案(0)|浏览(236)

我们正在开发一个使用spring boot和dgs netflix graphql的项目。我们创建了所有模式和datafethers,这些模式和datafethers在默认端点“/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();

    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题