Search in sources :

Example 11 with QueryAnalyzerResponseDTO

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;
}
Also used : GraphQL(graphql.GraphQL) GraphQLError(graphql.GraphQLError) ExecutionResult(graphql.ExecutionResult) QueryAnalyzerResponseDTO(org.wso2.carbon.apimgt.common.gateway.dto.QueryAnalyzerResponseDTO) MaxQueryComplexityInstrumentation(graphql.analysis.MaxQueryComplexityInstrumentation)

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