Search in sources :

Example 1 with MaxQueryComplexityInstrumentation

use of graphql.analysis.MaxQueryComplexityInstrumentation 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

ExecutionResult (graphql.ExecutionResult)1 GraphQL (graphql.GraphQL)1 GraphQLError (graphql.GraphQLError)1 MaxQueryComplexityInstrumentation (graphql.analysis.MaxQueryComplexityInstrumentation)1 QueryAnalyzerResponseDTO (org.wso2.carbon.apimgt.common.gateway.dto.QueryAnalyzerResponseDTO)1