Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.arangodb.graphql.create;

import com.arangodb.graphql.schema.ArangoVertexDirective;
import graphql.schema.*;

import java.util.ArrayList;
import java.util.List;

public class ArgumentIndexMapping {

private GraphQLFieldDefinition fieldDefinition;

private List<String> fields;

private String collection;

public ArgumentIndexMapping(GraphQLFieldDefinition fieldDefinition){
this.fieldDefinition = fieldDefinition;
init();
}


private void init(){

fields = new ArrayList<>();

List<GraphQLArgument> arguments = fieldDefinition.getArguments();

boolean firstMandatoryArgFound = false;
for(GraphQLArgument argument : arguments){
if(firstMandatoryArgFound){
fields.add(argument.getName());
}
else {
boolean nonNull = GraphQLTypeUtil.isNonNull(argument.getType());
if (nonNull) {
firstMandatoryArgFound = true;
fields.add(0, argument.getName());
} else {
fields.add(argument.getName());
}
}

}

GraphQLUnmodifiedType type = GraphQLTypeUtil.unwrapAll(fieldDefinition.getType());
if(type instanceof GraphQLDirectiveContainer) {
ArangoVertexDirective vertexDirective = new ArangoVertexDirective((GraphQLDirectiveContainer)type);
this.collection = vertexDirective.getCollection();
}
}

public String getCollection() {
return collection;
}

public List<String> getFields() {
return fields;
}
}
123 changes: 123 additions & 0 deletions src/main/java/com/arangodb/graphql/create/DatabaseObjectCreator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package com.arangodb.graphql.create;

import com.arangodb.ArangoDB;
import com.arangodb.ArangoDatabase;
import com.arangodb.entity.CollectionType;
import com.arangodb.graphql.schema.ArangoEdgeDirective;
import com.arangodb.graphql.schema.ArangoVertexDirective;
import com.arangodb.model.CollectionCreateOptions;
import com.arangodb.model.HashIndexOptions;
import graphql.schema.GraphQLArgument;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;
import graphql.schema.GraphQLType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

public class DatabaseObjectCreator {

private final Logger logger = LoggerFactory.getLogger(this.getClass());
private final ArangoDB arango;
private final String databaseName;
private final GraphQLSchema graphQLSchema;

public DatabaseObjectCreator(ArangoDB arango, String databaseName, GraphQLSchema graphQLSchema) {
this.arango = arango;
this.databaseName = databaseName;
this.graphQLSchema = graphQLSchema;
}

private List<TypeCollectionMapping> createTypeMappings(){

List<GraphQLType> types = graphQLSchema.getAllTypesAsList();

List<TypeCollectionMapping> mappings = new ArrayList<>();
types.forEach(type -> {
if(type instanceof GraphQLObjectType){
GraphQLObjectType objectType = (GraphQLObjectType) type;
ArangoVertexDirective vertexDirective = new ArangoVertexDirective(objectType);
String collection = vertexDirective.getCollection();
if(collection != null){
TypeCollectionMapping mapping = new TypeCollectionMapping(collection);

objectType.getFieldDefinitions().forEach(graphQLFieldDefinition -> {
ArangoEdgeDirective arangoEdgeDirective = new ArangoEdgeDirective(graphQLFieldDefinition);
String edgeCollectionName = arangoEdgeDirective.getCollection();
if(edgeCollectionName != null){
mapping.addEdgeCollection(edgeCollectionName);
}
});

mappings.add(mapping);

}
}
});

return mappings;

}

public void createDatabase(){
ArangoDatabase db = arango.db(databaseName);
if(!db.exists()){
logger.info("Auto Create Database {}", databaseName);
db.create();
}
else{
logger.info("Database {} already exists", databaseName);
}
}

public void createCollections(){

List<TypeCollectionMapping> mappings = createTypeMappings();
mappings.forEach(mapping -> {
String vertex = mapping.getVertexCollection();
createCollection(vertex, CollectionType.DOCUMENT);
Set<String> edgeCollections = mapping.getEdgeCollections();
edgeCollections.forEach(edgeCollection -> {
createCollection(edgeCollection, CollectionType.EDGES);
});
});

}

public void createIndexes(){

ArangoDatabase database = arango.db(databaseName);
GraphQLObjectType queryType = graphQLSchema.getQueryType();
queryType.getFieldDefinitions().forEach(graphQLFieldDefinition -> {
ArgumentIndexMapping mapping = new ArgumentIndexMapping(graphQLFieldDefinition);

String collection = mapping.getCollection();
if(collection != null){
logger.info("Auto Create Index on collection {} for fields {}", collection, mapping.getFields());
database.collection(collection).ensureHashIndex(mapping.getFields(), new HashIndexOptions());
}

});
}

protected CollectionCreateOptions createCollectionCreateOptions(CollectionType collectionType){
return new CollectionCreateOptions().type(collectionType);
}

private void createCollection(String name, CollectionType collectionType) {

CollectionCreateOptions collectionCreateOptions = createCollectionCreateOptions(collectionType);
ArangoDatabase database = arango.db(databaseName);

if(!database.collection(name).exists()){
logger.info("Auto Create Collection {}:{}", name, collectionType);
database.collection(name).create(collectionCreateOptions);
}
else{
logger.info("Collection already exists {}:{}", name, collectionType);
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.arangodb.graphql.create;

import java.util.HashSet;
import java.util.Set;

public class TypeCollectionMapping {

private final String vertexCollection;

private final Set<String> edgeCollections;

public TypeCollectionMapping(String vertexCollection){
this.vertexCollection = vertexCollection;
this.edgeCollections = new HashSet<>();
}

public void addEdgeCollection(String edgeCollection){
this.edgeCollections.add(edgeCollection);
}


public String getVertexCollection() {
return vertexCollection;
}

public Set<String> getEdgeCollections() {
return edgeCollections;
}
}
Loading