use of graphql.analysis.FieldComplexityCalculator in project carbon-apimgt by wso2.
the class QueryMutationAnalyzer method analyseQueryMutationComplexity.
/**
* This method analyses the query complexity
*
* @param messageContext message context of the request
* @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 boolean analyseQueryMutationComplexity(MessageContext messageContext, String payload) {
FieldComplexityCalculator fieldComplexityCalculator = null;
try {
fieldComplexityCalculator = new FieldComplexityCalculatorImpl((String) messageContext.getProperty(APIConstants.GRAPHQL_ACCESS_CONTROL_POLICY));
} catch (ParseException e) {
String errorMessage = "Policy definition parsing failed. ";
handleFailure(GraphQLConstants.GRAPHQL_INVALID_QUERY, messageContext, errorMessage, errorMessage);
}
int maxQueryComplexity = getMaxQueryComplexity(messageContext);
QueryAnalyzerResponseDTO responseDTO = analyseQueryComplexity(maxQueryComplexity, payload, fieldComplexityCalculator);
if (!responseDTO.isSuccess() && !responseDTO.getErrorList().isEmpty()) {
handleFailure(GraphQLConstants.GRAPHQL_QUERY_TOO_COMPLEX, messageContext, GraphQLConstants.GRAPHQL_QUERY_TOO_COMPLEX_MESSAGE, responseDTO.getErrorList().toString());
log.error(responseDTO.getErrorList().toString());
return false;
}
return true;
}
use of graphql.analysis.FieldComplexityCalculator in project carbon-apimgt by wso2.
the class SubscriptionAnalyzer method analyseSubscriptionQueryComplexity.
/**
* This method analyses the query complexity.
*
* @param payload Payload of the request
* @param maxQueryComplexity Maximum query complexity
* @return true, if query complexity does not exceed the maximum or false, if query complexity exceeds the maximum
*/
public QueryAnalyzerResponseDTO analyseSubscriptionQueryComplexity(String payload, int maxQueryComplexity) throws APIManagementException {
FieldComplexityCalculator fieldComplexityCalculator;
try {
// get access control policy
String accessControlInfo = getGraphQLAccessControlInfo();
fieldComplexityCalculator = new FieldComplexityCalculatorImpl(accessControlInfo);
} catch (ParseException e) {
throw new APIManagementException("Error while parsing policy definition.", e);
}
int updatedMaxQueryComplexity = getMaxQueryComplexity(maxQueryComplexity);
return analyseQueryComplexity(updatedMaxQueryComplexity, payload, fieldComplexityCalculator);
}
Aggregations