Search in sources :

Example 1 with MaxQueryDepthInstrumentation

use of graphql.analysis.MaxQueryDepthInstrumentation in project carbon-apimgt by wso2.

the class QueryAnalyzer method analyseQueryDepth.

/**
 * This method analyses the query depth.
 *
 * @param maxQueryDepth maximum query depth
 * @param payload       payload of the request
 * @return true, if the query depth does not exceed the maximum value or false, if query depth exceeds the maximum
 */
public QueryAnalyzerResponseDTO analyseQueryDepth(int maxQueryDepth, String payload) {
    if (log.isDebugEnabled()) {
        log.debug("Analyzing query depth for " + payload + " and max query depth:" + maxQueryDepth);
    }
    QueryAnalyzerResponseDTO queryAnalyzerResponseDTO = new QueryAnalyzerResponseDTO();
    // If maxQueryDepth is a positive value, perform the depth limitation check. Otherwise, bypass the check.
    if (maxQueryDepth > 0) {
        MaxQueryDepthInstrumentation maxQueryDepthInstrumentation = new MaxQueryDepthInstrumentation(maxQueryDepth);
        GraphQL runtime = GraphQL.newGraphQL(schema).instrumentation(maxQueryDepthInstrumentation).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 depth of " + maxQueryDepth + " was not exceeded");
                }
                queryAnalyzerResponseDTO.setSuccess(true);
                return queryAnalyzerResponseDTO;
            }
            log.error(queryAnalyzerResponseDTO.getErrorList().toString());
            queryAnalyzerResponseDTO.setSuccess(false);
            return queryAnalyzerResponseDTO;
        }
    }
    queryAnalyzerResponseDTO.setSuccess(true);
    return queryAnalyzerResponseDTO;
}
Also used : MaxQueryDepthInstrumentation(graphql.analysis.MaxQueryDepthInstrumentation) GraphQL(graphql.GraphQL) GraphQLError(graphql.GraphQLError) ExecutionResult(graphql.ExecutionResult) QueryAnalyzerResponseDTO(org.wso2.carbon.apimgt.common.gateway.dto.QueryAnalyzerResponseDTO)

Aggregations

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