Search in sources :

Example 1 with GraphQLSchemaTypeListDTO

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

the class ApisApiServiceImpl method getGraphQLPolicyComplexityTypesOfAPI.

/**
 * Get all types and fields of the GraphQL Schema of a given API
 *
 * @param apiId          apiId
 * @param messageContext message context
 * @return Response with all the types and fields found within the schema definition
 */
@Override
public Response getGraphQLPolicyComplexityTypesOfAPI(String apiId, MessageContext messageContext) {
    GraphQLSchemaDefinition graphql = new GraphQLSchemaDefinition();
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        APIIdentifier apiIdentifier;
        if (ApiMgtDAO.getInstance().checkAPIUUIDIsARevisionUUID(apiId) != null) {
            apiIdentifier = APIMappingUtil.getAPIInfoFromUUID(apiId, organization).getId();
        } else {
            apiIdentifier = APIMappingUtil.getAPIIdentifierFromUUID(apiId);
        }
        API api = apiProvider.getAPIbyUUID(apiId, organization);
        if (APIConstants.GRAPHQL_API.equals(api.getType())) {
            String schemaContent = apiProvider.getGraphqlSchema(apiIdentifier);
            List<GraphqlSchemaType> typeList = graphql.extractGraphQLTypeList(schemaContent);
            GraphQLSchemaTypeListDTO graphQLSchemaTypeListDTO = GraphqlQueryAnalysisMappingUtil.fromGraphqlSchemaTypeListtoDTO(typeList);
            return Response.ok().entity(graphQLSchemaTypeListDTO).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 types and fields of API : " + apiId, e, log);
        } else {
            String msg = "Error while retrieving types and fields of the schema of API " + apiId;
            RestApiUtil.handleInternalServerError(msg, e, log);
        }
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) GraphqlSchemaType(org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.GraphqlSchemaType) GraphQLSchemaTypeListDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.GraphQLSchemaTypeListDTO) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) API(org.wso2.carbon.apimgt.api.model.API) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) GraphQLSchemaDefinition(org.wso2.carbon.apimgt.impl.definitions.GraphQLSchemaDefinition) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 2 with GraphQLSchemaTypeListDTO

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

the class GraphqlQueryAnalysisMappingUtil method fromGraphqlSchemaTypeListtoDTO.

/**
 * Converts a list of GraphqlSchemaType objects into a DTO object.
 *
 * @param typeList List<GraphqlSchemaType>
 * @return a new GraphQLSchemaTypeListDTO object corresponding to given list of GraphqlSchemaType objects
 */
public static GraphQLSchemaTypeListDTO fromGraphqlSchemaTypeListtoDTO(List<GraphqlSchemaType> typeList) {
    GraphQLSchemaTypeListDTO graphQLSchemaTypeListDTO = new GraphQLSchemaTypeListDTO();
    List<GraphQLSchemaTypeDTO> graphQLSchemaTypeDTOList = new ArrayList<>();
    for (GraphqlSchemaType graphqlSchemaType : typeList) {
        GraphQLSchemaTypeDTO graphQLSchemaTypeDTO = new GraphQLSchemaTypeDTO();
        List<String> fieldList = new ArrayList<>(graphqlSchemaType.getFieldList());
        graphQLSchemaTypeDTO.setType(graphqlSchemaType.getType());
        graphQLSchemaTypeDTO.setFieldList(fieldList);
        graphQLSchemaTypeDTOList.add(graphQLSchemaTypeDTO);
    }
    graphQLSchemaTypeListDTO.setTypeList(graphQLSchemaTypeDTOList);
    return graphQLSchemaTypeListDTO;
}
Also used : GraphqlSchemaType(org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.GraphqlSchemaType) GraphQLSchemaTypeListDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.GraphQLSchemaTypeListDTO) ArrayList(java.util.ArrayList) GraphQLSchemaTypeDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.GraphQLSchemaTypeDTO)

Example 3 with GraphQLSchemaTypeListDTO

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

the class ApisApiServiceImpl method apisApiIdGraphqlPoliciesComplexityTypesGet.

@Override
public Response apisApiIdGraphqlPoliciesComplexityTypesGet(String apiId, MessageContext messageContext) throws APIManagementException {
    GraphQLSchemaDefinition graphql = new GraphQLSchemaDefinition();
    try {
        APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        APIIdentifier apiIdentifier = APIMappingUtil.getAPIIdentifierFromUUID(apiId, organization);
        API api = apiConsumer.getLightweightAPIByUUID(apiId, organization);
        if (APIConstants.GRAPHQL_API.equals(api.getType())) {
            String schemaContent = apiConsumer.getGraphqlSchema(apiIdentifier);
            List<GraphqlSchemaType> typeList = graphql.extractGraphQLTypeList(schemaContent);
            GraphQLSchemaTypeListDTO graphQLSchemaTypeListDTO = GraphqlQueryAnalysisMappingUtil.fromGraphqlSchemaTypeListtoDTO(typeList);
            return Response.ok().entity(graphQLSchemaTypeListDTO).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 types and fields of API : " + apiId, e, log);
        } else {
            String msg = "Error while retrieving types and fields of the schema of API " + apiId;
            RestApiUtil.handleInternalServerError(msg, e, log);
        }
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) GraphqlSchemaType(org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.GraphqlSchemaType) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) API(org.wso2.carbon.apimgt.api.model.API) GraphQLSchemaDefinition(org.wso2.carbon.apimgt.impl.definitions.GraphQLSchemaDefinition) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer)

Aggregations

GraphqlSchemaType (org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.GraphqlSchemaType)3 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)2 API (org.wso2.carbon.apimgt.api.model.API)2 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)2 GraphQLSchemaDefinition (org.wso2.carbon.apimgt.impl.definitions.GraphQLSchemaDefinition)2 GraphQLSchemaTypeListDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.GraphQLSchemaTypeListDTO)2 ArrayList (java.util.ArrayList)1 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)1 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)1 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)1 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)1 GraphQLSchemaTypeDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.GraphQLSchemaTypeDTO)1