Search in sources :

Example 1 with FieldComplexityCalculator

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;
}
Also used : FieldComplexityCalculator(graphql.analysis.FieldComplexityCalculator) FieldComplexityCalculatorImpl(org.wso2.carbon.apimgt.common.gateway.graphql.FieldComplexityCalculatorImpl) ParseException(org.json.simple.parser.ParseException) QueryAnalyzerResponseDTO(org.wso2.carbon.apimgt.common.gateway.dto.QueryAnalyzerResponseDTO)

Example 2 with FieldComplexityCalculator

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);
}
Also used : FieldComplexityCalculator(graphql.analysis.FieldComplexityCalculator) FieldComplexityCalculatorImpl(org.wso2.carbon.apimgt.common.gateway.graphql.FieldComplexityCalculatorImpl) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ParseException(org.json.simple.parser.ParseException)

Aggregations

FieldComplexityCalculator (graphql.analysis.FieldComplexityCalculator)2 ParseException (org.json.simple.parser.ParseException)2 FieldComplexityCalculatorImpl (org.wso2.carbon.apimgt.common.gateway.graphql.FieldComplexityCalculatorImpl)2 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1 QueryAnalyzerResponseDTO (org.wso2.carbon.apimgt.common.gateway.dto.QueryAnalyzerResponseDTO)1