use of org.wso2.carbon.apimgt.common.gateway.dto.QueryAnalyzerResponseDTO in project carbon-apimgt by wso2.
the class QueryAnalyzer method analyseQueryComplexity.
/**
* This method analyses the query complexity.
*
* @param fieldComplexityCalculator Field Complexity Calculator
* @param maxQueryComplexity Maximum query complexity value
* @param payload payload of the request
* @return true, if query complexity does not exceed the maximum or false, if query complexity exceeds the maximum
*/
public QueryAnalyzerResponseDTO analyseQueryComplexity(int maxQueryComplexity, String payload, FieldComplexityCalculator fieldComplexityCalculator) {
if (log.isDebugEnabled()) {
log.debug("Analyzing query complexity for " + payload + " and max complexity: " + maxQueryComplexity);
}
QueryAnalyzerResponseDTO queryAnalyzerResponseDTO = new QueryAnalyzerResponseDTO();
// Otherwise, bypass the check.
if (maxQueryComplexity > 0) {
MaxQueryComplexityInstrumentation maxQueryComplexityInstrumentation = new MaxQueryComplexityInstrumentation(maxQueryComplexity, fieldComplexityCalculator);
GraphQL runtime = GraphQL.newGraphQL(schema).instrumentation(maxQueryComplexityInstrumentation).build();
ExecutionResult executionResult = runtime.execute(payload);
List<GraphQLError> errors = executionResult.getErrors();
if (errors.size() > 0) {
for (GraphQLError error : errors) {
queryAnalyzerResponseDTO.addErrorToList((error.getMessage()));
}
// TODO: https://github.com/wso2/carbon-apimgt/issues/8147
queryAnalyzerResponseDTO.getErrorList().removeIf(s -> s.contains("non-nullable"));
if (queryAnalyzerResponseDTO.getErrorList().size() == 0) {
if (log.isDebugEnabled()) {
log.debug("Maximum query complexity was not exceeded");
}
queryAnalyzerResponseDTO.setSuccess(true);
} else {
log.error(queryAnalyzerResponseDTO.getErrorList());
queryAnalyzerResponseDTO.getErrorList().clear();
queryAnalyzerResponseDTO.addErrorToList("maximum query complexity exceeded");
}
queryAnalyzerResponseDTO.setSuccess(false);
return queryAnalyzerResponseDTO;
}
}
queryAnalyzerResponseDTO.setSuccess(true);
return queryAnalyzerResponseDTO;
}
Aggregations