use of graphql.schema.validation.SchemaValidationError 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;
}
Aggregations