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;
}
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;
}
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;
}
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;
}
Aggregations