Search in sources :

Example 1 with QueryAnalyzerResponseDTO

use of org.wso2.carbon.apimgt.common.gateway.dto.QueryAnalyzerResponseDTO 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 QueryAnalyzerResponseDTO

use of org.wso2.carbon.apimgt.common.gateway.dto.QueryAnalyzerResponseDTO in project carbon-apimgt by wso2.

the class QueryAnalyzerTestCase method testAnalyzeQueryDepthForSubscriptionsSuccess.

@Test
public void testAnalyzeQueryDepthForSubscriptionsSuccess() {
    int maxComplexity = 4;
    String payload = "subscription {\n" + "  liftStatusChange {\n" + "    name\n" + "  }\n" + "}\n";
    QueryAnalyzerResponseDTO queryAnalyzerResponseDTO = queryAnalyzer.analyseQueryComplexity(maxComplexity, payload, fieldComplexityCalculator);
    Assert.assertTrue(queryAnalyzerResponseDTO.isSuccess());
    Assert.assertTrue(queryAnalyzerResponseDTO.getErrorList().isEmpty());
    maxComplexity = 0;
    queryAnalyzerResponseDTO = queryAnalyzer.analyseQueryComplexity(maxComplexity, payload, fieldComplexityCalculator);
    Assert.assertTrue(queryAnalyzerResponseDTO.isSuccess());
    Assert.assertTrue(queryAnalyzerResponseDTO.getErrorList().isEmpty());
}
Also used : QueryAnalyzerResponseDTO(org.wso2.carbon.apimgt.common.gateway.dto.QueryAnalyzerResponseDTO) Test(org.junit.Test)

Example 3 with QueryAnalyzerResponseDTO

use of org.wso2.carbon.apimgt.common.gateway.dto.QueryAnalyzerResponseDTO in project carbon-apimgt by wso2.

the class QueryAnalyzerTestCase method testAnalyseQueryDepthForSubscriptionsError.

@Test
public void testAnalyseQueryDepthForSubscriptionsError() {
    int maxDepth = 1;
    String payload = "subscription {\n" + "  liftStatusChange {\n" + "    name\n" + "    id\n" + "  }\n" + "}\n";
    QueryAnalyzerResponseDTO queryAnalyzerResponseDTO = queryAnalyzer.analyseQueryDepth(maxDepth, payload);
    Assert.assertFalse(queryAnalyzerResponseDTO.isSuccess());
    Assert.assertTrue(queryAnalyzerResponseDTO.getErrorList().toString().contains("maximum query depth exceeded 2 > 1"));
}
Also used : QueryAnalyzerResponseDTO(org.wso2.carbon.apimgt.common.gateway.dto.QueryAnalyzerResponseDTO) Test(org.junit.Test)

Example 4 with QueryAnalyzerResponseDTO

use of org.wso2.carbon.apimgt.common.gateway.dto.QueryAnalyzerResponseDTO in project carbon-apimgt by wso2.

the class QueryAnalyzerTestCase method testAnalyzeQueryDepthForSubscriptionsError.

@Test
public void testAnalyzeQueryDepthForSubscriptionsError() {
    int maxComplexity = 4;
    String payload = "subscription {\n" + "  liftStatusChange {\n" + "    name\n" + "    id\n" + "    night\n" + "  }\n" + "}\n";
    QueryAnalyzerResponseDTO queryAnalyzerResponseDTO = queryAnalyzer.analyseQueryComplexity(maxComplexity, payload, fieldComplexityCalculator);
    Assert.assertFalse(queryAnalyzerResponseDTO.isSuccess());
    Assert.assertTrue(queryAnalyzerResponseDTO.getErrorList().toString().contains("maximum query complexity exceeded"));
}
Also used : QueryAnalyzerResponseDTO(org.wso2.carbon.apimgt.common.gateway.dto.QueryAnalyzerResponseDTO) Test(org.junit.Test)

Example 5 with QueryAnalyzerResponseDTO

use of org.wso2.carbon.apimgt.common.gateway.dto.QueryAnalyzerResponseDTO in project carbon-apimgt by wso2.

the class QueryAnalyzerTestCase method testAnalyseQueryDepthForSubscriptionsSuccess.

@Test
public void testAnalyseQueryDepthForSubscriptionsSuccess() {
    int maxDepth = 3;
    String payload = "subscription {\n" + "  liftStatusChange {\n" + "    id\n" + "  }\n" + "}\n";
    QueryAnalyzerResponseDTO queryAnalyzerResponseDTO = queryAnalyzer.analyseQueryDepth(maxDepth, payload);
    Assert.assertTrue(queryAnalyzerResponseDTO.isSuccess());
    Assert.assertTrue(queryAnalyzerResponseDTO.getErrorList().isEmpty());
    maxDepth = 0;
    queryAnalyzerResponseDTO = queryAnalyzer.analyseQueryDepth(maxDepth, payload);
    Assert.assertTrue(queryAnalyzerResponseDTO.isSuccess());
    Assert.assertTrue(queryAnalyzerResponseDTO.getErrorList().isEmpty());
}
Also used : QueryAnalyzerResponseDTO(org.wso2.carbon.apimgt.common.gateway.dto.QueryAnalyzerResponseDTO) Test(org.junit.Test)

Aggregations

QueryAnalyzerResponseDTO (org.wso2.carbon.apimgt.common.gateway.dto.QueryAnalyzerResponseDTO)10 Test (org.junit.Test)4 ExecutionResult (graphql.ExecutionResult)2 GraphQL (graphql.GraphQL)2 GraphQLError (graphql.GraphQLError)2 FieldComplexityCalculator (graphql.analysis.FieldComplexityCalculator)2 ParseException (org.json.simple.parser.ParseException)2 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)2 FieldComplexityCalculatorImpl (org.wso2.carbon.apimgt.common.gateway.graphql.FieldComplexityCalculatorImpl)2 GraphQLProcessorResponseDTO (org.wso2.carbon.apimgt.gateway.inbound.websocket.GraphQLProcessorResponseDTO)2 MaxQueryComplexityInstrumentation (graphql.analysis.MaxQueryComplexityInstrumentation)1 MaxQueryDepthInstrumentation (graphql.analysis.MaxQueryDepthInstrumentation)1