Skip to content

Commit 3a56d58

Browse files
author
michele
committed
initial
1 parent cafed60 commit 3a56d58

File tree

82 files changed

+7191
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+7191
-0
lines changed

.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,17 @@
2121

2222
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
2323
hs_err_pid*
24+
25+
26+
target
27+
28+
#idea-generated
29+
.idea
30+
*.iml
31+
32+
#eclipse-generated
33+
.settings/
34+
.classpath
35+
.project
36+
37+
node_modules

pom.xml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ DISCLAIMER
4+
~ Copyright 2019 ArangoDB GmbH, Cologne, Germany
5+
~
6+
~ Licensed under the Apache License, Version 2.0 (the "License");
7+
~ you may not use this file except in compliance with the License.
8+
~ You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing, software
13+
~ distributed under the License is distributed on an "AS IS" BASIS,
14+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ See the License for the specific language governing permissions and
16+
~ limitations under the License.
17+
~
18+
~ Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
~
20+
-->
21+
22+
<project xmlns="http://maven.apache.org/POM/4.0.0"
23+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
24+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
25+
<modelVersion>4.0.0</modelVersion>
26+
27+
<groupId>com.arangodb</groupId>
28+
<artifactId>arangodb-graphql</artifactId>
29+
<version>1.0-SNAPSHOT</version>
30+
31+
<properties>
32+
<java.version>1.8</java.version>
33+
<maven.compiler.source>1.8</maven.compiler.source>
34+
<maven.compiler.target>1.8</maven.compiler.target>
35+
</properties>
36+
37+
<dependencies>
38+
<dependency>
39+
<groupId>com.graphql-java</groupId>
40+
<artifactId>graphql-java</artifactId>
41+
<version>11.0</version>
42+
</dependency>
43+
<dependency>
44+
<groupId>com.arangodb</groupId>
45+
<artifactId>arangodb-java-driver</artifactId>
46+
<version>6.0.0</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>junit</groupId>
50+
<artifactId>junit</artifactId>
51+
<version>4.12</version>
52+
<scope>test</scope>
53+
</dependency>
54+
<dependency>
55+
<groupId>org.mockito</groupId>
56+
<artifactId>mockito-core</artifactId>
57+
<version>2.15.0</version>
58+
<scope>test</scope>
59+
</dependency>
60+
<dependency>
61+
<groupId>org.hamcrest</groupId>
62+
<artifactId>hamcrest-library</artifactId>
63+
<version>1.3</version>
64+
<scope>test</scope>
65+
</dependency>
66+
</dependencies>
67+
68+
</project>
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* DISCLAIMER
3+
* Copyright 2019 ArangoDB GmbH, Cologne, Germany
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
* Copyright holder is ArangoDB GmbH, Cologne, Germany
18+
*
19+
*/
20+
21+
package com.arangodb.graphql;
22+
23+
import com.arangodb.graphql.context.ArangoGraphQLContext;
24+
import com.arangodb.graphql.context.SelectedFieldCollector;
25+
import com.arangodb.graphql.generator.ArangoQueryGeneratorChain;
26+
import com.arangodb.graphql.query.ArangoTraversalQuery;
27+
import com.arangodb.graphql.query.ArangoTraversalQueryExecutor;
28+
import com.arangodb.graphql.query.result.ArangoTraversalQueryResult;
29+
import graphql.schema.DataFetcher;
30+
import graphql.schema.DataFetchingEnvironment;
31+
import org.slf4j.Logger;
32+
import org.slf4j.LoggerFactory;
33+
34+
import java.time.Duration;
35+
import java.time.Instant;
36+
import java.util.List;
37+
import java.util.Map;
38+
import java.util.function.Function;
39+
40+
/**
41+
* A DataFetcher implementation that
42+
*
43+
* 1. Converts the incoming GraphQL query to a native AQL query
44+
* 2. Executes the query on Arango
45+
* 3. Marshals the result of a collection of paths from Arango back to the format prescribed by the GraphQL query
46+
*
47+
* @author Colin Findlay
48+
*
49+
*/
50+
public class ArangoDataFetcher implements DataFetcher {
51+
52+
private final Logger logger = LoggerFactory.getLogger(this.getClass());
53+
54+
private ArangoQueryGeneratorChain queryGenerator;
55+
56+
private ArangoTraversalQueryExecutor queryExecutor;
57+
58+
/**
59+
* Default Constructor
60+
* @param queryGenerator A query generator chain
61+
* @param queryExecutor A query executor
62+
*/
63+
public ArangoDataFetcher(ArangoQueryGeneratorChain queryGenerator, ArangoTraversalQueryExecutor queryExecutor) {
64+
this.queryGenerator = queryGenerator;
65+
this.queryExecutor = queryExecutor;
66+
}
67+
68+
private ArangoTraversalQuery createQuery(DataFetchingEnvironment dataFetchingEnvironment){
69+
logger.debug("Converting incoming GraphQL Query to AQL Query");
70+
Instant start = Instant.now();
71+
SelectedFieldCollector collector = new SelectedFieldCollector(dataFetchingEnvironment);
72+
ArangoGraphQLContext ctx = new ArangoGraphQLContext(dataFetchingEnvironment, collector);
73+
ArangoTraversalQuery query = queryGenerator.execute(ctx);
74+
Instant finish = Instant.now();
75+
long duration = Duration.between(start, finish).toMillis();
76+
logger.debug("Query Generation Completed in {}ms", duration);
77+
logger.trace("Generated Query: \n{}", query.getAql());
78+
return query;
79+
}
80+
81+
private ArangoTraversalQueryResult executeQuery(ArangoTraversalQuery query){
82+
logger.debug("Begin Query Execution and Result Processing");
83+
Instant start = Instant.now();
84+
ArangoTraversalQueryResult result = queryExecutor.execute(query);
85+
Instant finish = Instant.now();
86+
long duration = Duration.between(start, finish).toMillis();
87+
logger.debug("Query Execution and Result Processing Completed in {}ms", duration);
88+
logger.debug("Query returned {} paths", result.size());
89+
return result;
90+
}
91+
92+
private List<Map> processResult(ArangoTraversalQueryResult result){
93+
logger.debug("Converting paths to GraphQL Query Response");
94+
Instant start = Instant.now();
95+
List<Map> convertedResult = result.getResult();
96+
Instant finish = Instant.now();
97+
long duration = Duration.between(start, finish).toMillis();
98+
logger.debug("Response conversion completed in {}ms", duration);
99+
logger.debug("GraphQL Query Response has {} elements", convertedResult.size());
100+
return convertedResult;
101+
}
102+
103+
@Override
104+
public Object get(DataFetchingEnvironment dataFetchingEnvironment) throws Exception {
105+
106+
ArangoTraversalQuery query = createQuery(dataFetchingEnvironment);
107+
ArangoTraversalQueryResult result = executeQuery(query);
108+
return processResult(result);
109+
}
110+
111+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* DISCLAIMER
3+
* Copyright 2019 ArangoDB GmbH, Cologne, Germany
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
* Copyright holder is ArangoDB GmbH, Cologne, Germany
18+
*
19+
*/
20+
21+
package com.arangodb.graphql.context;
22+
23+
import com.arangodb.model.TraversalOptions;
24+
25+
import java.util.HashSet;
26+
import java.util.Set;
27+
28+
/**
29+
* Class that represents the traversal direction of an edge.
30+
*
31+
* @author Colin Findlay
32+
*/
33+
public class ArangoEdgeTraversalDirection {
34+
35+
private Set<TraversalOptions.Direction> directions;
36+
37+
public ArangoEdgeTraversalDirection() {
38+
directions = new HashSet<>();
39+
}
40+
41+
/**
42+
* Push a direction into a set of all directions for this edge
43+
*
44+
* @param direction The direction of this edge
45+
*/
46+
public void push(TraversalOptions.Direction direction) {
47+
directions.add(direction);
48+
}
49+
50+
/**
51+
* Compute the direction of traversals on this edge
52+
*
53+
* @return If the set contains both OUTBOUND and INBOUND directions the direction will be any, if it contains only
54+
* INBOUND the driection will be INBOUND, otherwise the direction will be OUTBOUND
55+
*/
56+
public TraversalOptions.Direction direction() {
57+
58+
if (isAny()) {
59+
return TraversalOptions.Direction.any;
60+
}
61+
62+
if (isInbound()) {
63+
return TraversalOptions.Direction.inbound;
64+
}
65+
66+
return TraversalOptions.Direction.outbound;
67+
68+
}
69+
70+
private boolean isAny() {
71+
return directions.contains(TraversalOptions.Direction.any) ||
72+
(directions.contains(TraversalOptions.Direction.inbound) &&
73+
directions.contains(TraversalOptions.Direction.outbound));
74+
}
75+
76+
private boolean isInbound() {
77+
return directions.contains(TraversalOptions.Direction.inbound);
78+
}
79+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* DISCLAIMER
3+
* Copyright 2019 ArangoDB GmbH, Cologne, Germany
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
* Copyright holder is ArangoDB GmbH, Cologne, Germany
18+
*
19+
*/
20+
21+
package com.arangodb.graphql.context;
22+
23+
import java.util.ArrayList;
24+
import java.util.Arrays;
25+
import java.util.Collection;
26+
import java.util.List;
27+
28+
/**
29+
* This class represents a filter, filtering a specific key for one of the supplied values
30+
*
31+
* @author Colin Findlay
32+
*/
33+
public class ArangoFilter {
34+
35+
private final String key;
36+
37+
private final List values;
38+
39+
public ArangoFilter(String key, Object value) {
40+
this.key = key;
41+
if (value instanceof Collection) {
42+
this.values = new ArrayList((Collection) value);
43+
} else {
44+
this.values = Arrays.asList(value);
45+
}
46+
}
47+
48+
public String getKey() {
49+
return key;
50+
}
51+
52+
public List getValues() {
53+
return values;
54+
}
55+
56+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* DISCLAIMER
3+
* Copyright 2019 ArangoDB GmbH, Cologne, Germany
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
* Copyright holder is ArangoDB GmbH, Cologne, Germany
18+
*
19+
*/
20+
21+
package com.arangodb.graphql.context;
22+
23+
import java.util.List;
24+
import java.util.Map;
25+
import java.util.stream.Collectors;
26+
27+
/**
28+
* This class represents a group of filters
29+
* @author Colin Findlay
30+
* @see ArangoFilter
31+
*/
32+
public class ArangoFilterGroup {
33+
34+
private List<ArangoFilter> filters;
35+
36+
private int depth;
37+
38+
public ArangoFilterGroup(List<ArangoFilter> filters, int depth) {
39+
this.filters = filters;
40+
this.depth = depth;
41+
}
42+
43+
public ArangoFilterGroup(Map<String, Object> arguments, int depth) {
44+
this.filters = arguments.entrySet()
45+
.stream()
46+
.map(e -> new ArangoFilter(e.getKey(), e.getValue()))
47+
.collect(Collectors.toList());
48+
this.depth = depth;
49+
}
50+
51+
public int getDepth() {
52+
return depth;
53+
}
54+
55+
public List<ArangoFilter> getFilters() {
56+
return filters;
57+
}
58+
59+
}

0 commit comments

Comments
 (0)