Controller in DGS Netflix Graphql

1.8k views Asked by At

We are developing a project Using Spring boot with DGS Netflix graphql. We are created all the schemas and datafethers which is working absolutely fine with a default endpoint "/graphql". we would like to expose this app with custom endpoing so we are trying to add a controller with a custom endpoint as below. But When i run the application and send a query, my data fetcher is called twice . first time called from controller and second time i believe from framework it self. Anybody has any thoughts on this why its being called twice and how to avoid it? You help is highly appreciated. Please see the below Datafetcher and 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();
        
    }
}
0

There are 0 answers