Search in sources :

Example 1 with GraphQLValidationResponseDTO

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

the class PublisherCommonUtils method validateGraphQLSchema.

/**
 * Validate GraphQL Schema.
 *
 * @param filename file name of the schema
 * @param schema   GraphQL schema
 */
public static GraphQLValidationResponseDTO validateGraphQLSchema(String filename, String schema) throws APIManagementException {
    String errorMessage;
    GraphQLValidationResponseDTO validationResponse = new GraphQLValidationResponseDTO();
    boolean isValid = false;
    try {
        if (filename.endsWith(".graphql") || filename.endsWith(".txt") || filename.endsWith(".sdl")) {
            if (schema.isEmpty()) {
                throw new APIManagementException("GraphQL Schema cannot be empty or null to validate it", ExceptionCodes.GRAPHQL_SCHEMA_CANNOT_BE_NULL);
            }
            SchemaParser schemaParser = new SchemaParser();
            TypeDefinitionRegistry typeRegistry = schemaParser.parse(schema);
            GraphQLSchema graphQLSchema = UnExecutableSchemaGenerator.makeUnExecutableSchema(typeRegistry);
            SchemaValidator schemaValidation = new SchemaValidator();
            Set<SchemaValidationError> validationErrors = schemaValidation.validateSchema(graphQLSchema);
            if (validationErrors.toArray().length > 0) {
                errorMessage = "InValid Schema";
                validationResponse.isValid(Boolean.FALSE);
                validationResponse.errorMessage(errorMessage);
            } else {
                validationResponse.setIsValid(Boolean.TRUE);
                GraphQLValidationResponseGraphQLInfoDTO graphQLInfo = new GraphQLValidationResponseGraphQLInfoDTO();
                GraphQLSchemaDefinition graphql = new GraphQLSchemaDefinition();
                List<URITemplate> operationList = graphql.extractGraphQLOperationList(typeRegistry, null);
                List<APIOperationsDTO> operationArray = APIMappingUtil.fromURITemplateListToOprationList(operationList);
                graphQLInfo.setOperations(operationArray);
                GraphQLSchemaDTO schemaObj = new GraphQLSchemaDTO();
                schemaObj.setSchemaDefinition(schema);
                graphQLInfo.setGraphQLSchema(schemaObj);
                validationResponse.setGraphQLInfo(graphQLInfo);
            }
        } else {
            throw new APIManagementException("Unsupported extension type of file: " + filename, ExceptionCodes.UNSUPPORTED_GRAPHQL_FILE_EXTENSION);
        }
        isValid = validationResponse.isIsValid();
        errorMessage = validationResponse.getErrorMessage();
    } catch (SchemaProblem e) {
        errorMessage = e.getMessage();
    }
    if (!isValid) {
        validationResponse.setIsValid(isValid);
        validationResponse.setErrorMessage(errorMessage);
    }
    return validationResponse;
}
Also used : TypeDefinitionRegistry(graphql.schema.idl.TypeDefinitionRegistry) SchemaValidator(graphql.schema.validation.SchemaValidator) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) GraphQLSchemaDefinition(org.wso2.carbon.apimgt.impl.definitions.GraphQLSchemaDefinition) GraphQLValidationResponseDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.GraphQLValidationResponseDTO) SchemaParser(graphql.schema.idl.SchemaParser) GraphQLSchema(graphql.schema.GraphQLSchema) SchemaValidationError(graphql.schema.validation.SchemaValidationError) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) GraphQLValidationResponseGraphQLInfoDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.GraphQLValidationResponseGraphQLInfoDTO) SchemaProblem(graphql.schema.idl.errors.SchemaProblem) APIOperationsDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIOperationsDTO) GraphQLSchemaDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.GraphQLSchemaDTO)

Example 2 with GraphQLValidationResponseDTO

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

the class ApisApiServiceImpl method validateGraphQLSchema.

/**
 * Validate graphQL Schema
 * @param fileInputStream  input file
 * @param fileDetail file Detail
 * @param messageContext messageContext
 * @return Validation response
 */
@Override
public Response validateGraphQLSchema(InputStream fileInputStream, Attachment fileDetail, MessageContext messageContext) {
    GraphQLValidationResponseDTO validationResponse = new GraphQLValidationResponseDTO();
    String filename = fileDetail.getContentDisposition().getFilename();
    try {
        String schema = IOUtils.toString(fileInputStream, RestApiConstants.CHARSET);
        validationResponse = PublisherCommonUtils.validateGraphQLSchema(filename, schema);
    } catch (IOException | APIManagementException e) {
        validationResponse.setIsValid(false);
        validationResponse.setErrorMessage(e.getMessage());
    }
    return Response.ok().entity(validationResponse).build();
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) IOException(java.io.IOException) GraphQLValidationResponseDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.GraphQLValidationResponseDTO)

Example 3 with GraphQLValidationResponseDTO

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

the class ImportUtils method retrieveValidatedGraphqlSchemaFromArchive.

/**
 * Validate GraphQL Schema definition from the archive directory and return it.
 *
 * @param pathToArchive Path to API archive
 * @throws APIImportExportException If an error occurs while reading the file
 */
public static String retrieveValidatedGraphqlSchemaFromArchive(String pathToArchive) throws APIManagementException {
    File file = new File(pathToArchive + ImportExportConstants.GRAPHQL_SCHEMA_DEFINITION_LOCATION);
    try {
        String schemaDefinition = loadGraphqlSDLFile(pathToArchive);
        GraphQLValidationResponseDTO graphQLValidationResponseDTO = PublisherCommonUtils.validateGraphQLSchema(file.getName(), schemaDefinition);
        if (!graphQLValidationResponseDTO.isIsValid()) {
            throw new APIManagementException("Error occurred while importing the API. Invalid GraphQL schema definition found. " + graphQLValidationResponseDTO.getErrorMessage());
        }
        return schemaDefinition;
    } catch (IOException e) {
        throw new APIManagementException("Error while reading API meta information from path: " + pathToArchive, e, ExceptionCodes.ERROR_READING_META_DATA);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) IOException(java.io.IOException) GraphQLValidationResponseDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.GraphQLValidationResponseDTO) File(java.io.File)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)3 GraphQLValidationResponseDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.GraphQLValidationResponseDTO)3 IOException (java.io.IOException)2 GraphQLSchema (graphql.schema.GraphQLSchema)1 SchemaParser (graphql.schema.idl.SchemaParser)1 TypeDefinitionRegistry (graphql.schema.idl.TypeDefinitionRegistry)1 SchemaProblem (graphql.schema.idl.errors.SchemaProblem)1 SchemaValidationError (graphql.schema.validation.SchemaValidationError)1 SchemaValidator (graphql.schema.validation.SchemaValidator)1 File (java.io.File)1 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)1 GraphQLSchemaDefinition (org.wso2.carbon.apimgt.impl.definitions.GraphQLSchemaDefinition)1 APIOperationsDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIOperationsDTO)1 GraphQLSchemaDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.GraphQLSchemaDTO)1 GraphQLValidationResponseGraphQLInfoDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.GraphQLValidationResponseGraphQLInfoDTO)1