Search in sources :

Example 1 with GraphqlSchemaType

use of org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.GraphqlSchemaType 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 GraphqlSchemaType

use of org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.GraphqlSchemaType in project carbon-apimgt by wso2.

the class GraphQLSchemaDefinition method extractGraphQLTypeList.

/**
 * Extract GraphQL Types and Fields from given schema
 *
 * @param schema GraphQL Schema
 * @return list of all types and fields
 */
public List<GraphqlSchemaType> extractGraphQLTypeList(String schema) {
    List<GraphqlSchemaType> typeList = new ArrayList<>();
    SchemaParser schemaParser = new SchemaParser();
    TypeDefinitionRegistry typeRegistry = schemaParser.parse(schema);
    Map<java.lang.String, TypeDefinition> list = typeRegistry.types();
    for (Map.Entry<String, TypeDefinition> entry : list.entrySet()) {
        if (entry.getValue() instanceof ObjectTypeDefinition) {
            GraphqlSchemaType graphqlSchemaType = new GraphqlSchemaType();
            List<String> fieldList = new ArrayList<>();
            graphqlSchemaType.setType(entry.getValue().getName());
            for (FieldDefinition fieldDef : ((ObjectTypeDefinition) entry.getValue()).getFieldDefinitions()) {
                fieldList.add(fieldDef.getName());
            }
            graphqlSchemaType.setFieldList(fieldList);
            typeList.add(graphqlSchemaType);
        }
    }
    return typeList;
}
Also used : ObjectTypeDefinition(graphql.language.ObjectTypeDefinition) FieldDefinition(graphql.language.FieldDefinition) TypeDefinitionRegistry(graphql.schema.idl.TypeDefinitionRegistry) SchemaParser(graphql.schema.idl.SchemaParser) OperationTypeDefinition(graphql.language.OperationTypeDefinition) ObjectTypeDefinition(graphql.language.ObjectTypeDefinition) TypeDefinition(graphql.language.TypeDefinition) GraphqlSchemaType(org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.GraphqlSchemaType)

Example 3 with GraphqlSchemaType

use of org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.GraphqlSchemaType 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 4 with GraphqlSchemaType

use of org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.GraphqlSchemaType 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)4 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 FieldDefinition (graphql.language.FieldDefinition)1 ObjectTypeDefinition (graphql.language.ObjectTypeDefinition)1 OperationTypeDefinition (graphql.language.OperationTypeDefinition)1 TypeDefinition (graphql.language.TypeDefinition)1 SchemaParser (graphql.schema.idl.SchemaParser)1 TypeDefinitionRegistry (graphql.schema.idl.TypeDefinitionRegistry)1 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