use of graphql.analysis.MaxQueryDepthInstrumentation in project carbon-apimgt by wso2.
the class QueryAnalyzer method analyseQueryDepth.
/**
* This method analyses the query depth.
*
* @param maxQueryDepth maximum query depth
* @param payload payload of the request
* @return true, if the query depth does not exceed the maximum value or false, if query depth exceeds the maximum
*/
public QueryAnalyzerResponseDTO analyseQueryDepth(int maxQueryDepth, String payload) {
if (log.isDebugEnabled()) {
log.debug("Analyzing query depth for " + payload + " and max query depth:" + maxQueryDepth);
}
QueryAnalyzerResponseDTO queryAnalyzerResponseDTO = new QueryAnalyzerResponseDTO();
// If maxQueryDepth is a positive value, perform the depth limitation check. Otherwise, bypass the check.
if (maxQueryDepth > 0) {
MaxQueryDepthInstrumentation maxQueryDepthInstrumentation = new MaxQueryDepthInstrumentation(maxQueryDepth);
GraphQL runtime = GraphQL.newGraphQL(schema).instrumentation(maxQueryDepthInstrumentation).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 depth of " + maxQueryDepth + " was not exceeded");
}
queryAnalyzerResponseDTO.setSuccess(true);
return queryAnalyzerResponseDTO;
}
log.error(queryAnalyzerResponseDTO.getErrorList().toString());
queryAnalyzerResponseDTO.setSuccess(false);
return queryAnalyzerResponseDTO;
}
}
queryAnalyzerResponseDTO.setSuccess(true);
return queryAnalyzerResponseDTO;
}
Aggregations