Search in sources :

Example 6 with GraphQLQueryComplexityInfoDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.GraphQLQueryComplexityInfoDTO in project carbon-apimgt by wso2.

the class GraphqlQueryAnalysisMappingUtil method fromDTOtoValidatedGraphqlComplexityInfo.

/**
 * Converts a GraphQLQueryComplexityInfo DTO object into a GraphqlComplexityInfo object. During this process a
 * basic validation is done comparing with the types of the schema
 *
 * @param graphQLQueryComplexityInfoDTO GraphQLQueryComplexityInfoDTO object
 * @param schema                        GraphQL Schema
 * @return a new GraphqlComplexityInfo object corresponding to given GraphQLQueryComplexityInfoDTO object
 */
public static GraphqlComplexityInfo fromDTOtoValidatedGraphqlComplexityInfo(GraphQLQueryComplexityInfoDTO graphQLQueryComplexityInfoDTO, String schema) {
    SchemaParser schemaParser = new SchemaParser();
    Set<String> complexityInfoTypeSet = schemaParser.parse(schema).types().keySet();
    GraphqlComplexityInfo graphqlComplexityInfo = new GraphqlComplexityInfo();
    List<CustomComplexityDetails> customComplexityDetailsList = new ArrayList<CustomComplexityDetails>();
    for (GraphQLCustomComplexityInfoDTO graphQLCustomComplexityInfoDTO : graphQLQueryComplexityInfoDTO.getList()) {
        String complexityType = graphQLCustomComplexityInfoDTO.getType();
        if (complexityInfoTypeSet.contains(complexityType)) {
            CustomComplexityDetails customComplexityDetails = new CustomComplexityDetails();
            customComplexityDetails.setType(complexityType);
            customComplexityDetails.setField(graphQLCustomComplexityInfoDTO.getField());
            customComplexityDetails.setComplexityValue(graphQLCustomComplexityInfoDTO.getComplexityValue());
            customComplexityDetailsList.add(customComplexityDetails);
        } else {
            log.error("Complexity Type : " + complexityType + " is not included in the original schema. Hence " + "skipped.");
        }
    }
    graphqlComplexityInfo.setList(customComplexityDetailsList);
    return graphqlComplexityInfo;
}
Also used : GraphqlComplexityInfo(org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.GraphqlComplexityInfo) ArrayList(java.util.ArrayList) GraphQLCustomComplexityInfoDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.GraphQLCustomComplexityInfoDTO) SchemaParser(graphql.schema.idl.SchemaParser) CustomComplexityDetails(org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.CustomComplexityDetails)

Example 7 with GraphQLQueryComplexityInfoDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.GraphQLQueryComplexityInfoDTO in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdGraphqlPoliciesComplexityGet.

/**
 * Get complexity details of a given API
 *
 * @param apiId          apiId
 * @param messageContext message context
 * @return Response with complexity details of the GraphQL API
 */
@Override
public Response apisApiIdGraphqlPoliciesComplexityGet(String apiId, MessageContext messageContext) {
    try {
        APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        API api = apiConsumer.getLightweightAPIByUUID(apiId, organization);
        if (APIConstants.GRAPHQL_API.equals(api.getType())) {
            GraphqlComplexityInfo graphqlComplexityInfo = apiConsumer.getComplexityDetails(apiId);
            GraphQLQueryComplexityInfoDTO graphQLQueryComplexityInfoDTO = GraphqlQueryAnalysisMappingUtil.fromGraphqlComplexityInfotoDTO(graphqlComplexityInfo);
            return Response.ok().entity(graphQLQueryComplexityInfoDTO).build();
        } else {
            throw new APIManagementException(ExceptionCodes.API_NOT_GRAPHQL);
        }
    } catch (APIManagementException e) {
        // to expose the existence of the resource
        if (RestApiUtil.isDueToResourceNotFound(e) || RestApiUtil.isDueToAuthorizationFailure(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
        } else if (isAuthorizationFailure(e)) {
            RestApiUtil.handleAuthorizationFailure("Authorization failure while retrieving complexity details of API : " + apiId, e, log);
        } else {
            String msg = "Error while retrieving complexity details of API " + apiId;
            RestApiUtil.handleInternalServerError(msg, e, log);
        }
    }
    return null;
}
Also used : GraphqlComplexityInfo(org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.GraphqlComplexityInfo) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) API(org.wso2.carbon.apimgt.api.model.API) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer)

Aggregations

GraphqlComplexityInfo (org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.GraphqlComplexityInfo)6 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)5 API (org.wso2.carbon.apimgt.api.model.API)4 GraphQLQueryComplexityInfoDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.GraphQLQueryComplexityInfoDTO)4 Gson (com.google.gson.Gson)2 JsonElement (com.google.gson.JsonElement)2 JsonParser (com.google.gson.JsonParser)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)2 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)2 CustomComplexityDetails (org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.CustomComplexityDetails)2 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)2 GraphQLCustomComplexityInfoDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.GraphQLCustomComplexityInfoDTO)2 GsonBuilder (com.google.gson.GsonBuilder)1 JsonObject (com.google.gson.JsonObject)1 SchemaParser (graphql.schema.idl.SchemaParser)1 HashMap (java.util.HashMap)1 JSONObject (org.json.JSONObject)1 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)1