Skip to content

Commit 38cc743

Browse files
committed
[NOID] Fixes #4157: apoc.dv.queryAndLink how to specify direction (#4222)
* Fixes #4157: apoc.dv.queryAndLink how to specify direction * test fixes * refactoring
1 parent be96c18 commit 38cc743

File tree

5 files changed

+410
-139
lines changed

5 files changed

+410
-139
lines changed

docs/asciidoc/modules/ROOT/pages/overview/apoc.dv/apoc.dv.queryAndLink.adoc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,13 @@ apoc.dv.queryAndLink(node :: NODE?, relName :: STRING?, name :: STRING?, params
3535
|path|PATH?
3636
|===
3737

38+
== Configuration parameters
39+
40+
The procedures support the following config parameters:
41+
42+
.Config parameters
43+
[opts=header]
44+
|===
45+
| name | type | default | description
46+
| direction | String | "OUT" | The direction of the relationships, i.e. outgoing ("OUT") or incoming ("IN").
47+
|===

docs/asciidoc/modules/ROOT/pages/virtual-resource/index.adoc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,16 @@ phrase over the previous query.
113113

114114
image::apoc.dv.jdbc-queryAndLink.png[scaledwidth="100%"]
115115

116+
The default direction of the relationships is outgoing (i.e. `{direction: "OUT"}`), but it is possible to reverse it by the config parameters.
117+
Example:
118+
119+
[source,cypher]
120+
----
121+
MATCH (hook:Hook) WITH hook
122+
CALL apoc.dv.queryAndLink(hook, $relType, $name, $queryParams, { direction: "IN" }) yield path
123+
RETURN path
124+
----
125+
116126
=== Listing the Virtualized Resource Catalog
117127
The apoc.dv.catalog.list procedure returns a list with all the existing Virtualized resources and their descriptions. It takes no parameters.
118128

full/src/main/java/apoc/dv/DataVirtualizationCatalog.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import apoc.result.VirtualPath;
2626
import apoc.result.VirtualRelationship;
2727
import java.util.Map;
28+
import java.util.Objects;
2829
import java.util.stream.Stream;
2930
import org.neo4j.graphdb.GraphDatabaseService;
3031
import org.neo4j.graphdb.Node;
@@ -103,10 +104,30 @@ public Stream<PathResult> queryAndLink(
103104
VirtualizedResource vr = new DataVirtualizationCatalogHandler(db, apocConfig.getSystemDb(), null).get(name);
104105
final RelationshipType relationshipType = RelationshipType.withName(relName);
105106
final Pair<String, Map<String, Object>> procedureCallWithParams = vr.getProcedureCallWithParams(params, config);
107+
108+
String direction = (String) config.getOrDefault("direction", Direction.OUT.name());
109+
106110
return tx.execute(procedureCallWithParams.first(), procedureCallWithParams.other()).stream()
107111
.map(m -> (Node) m.get(("node")))
108-
.map(n -> new VirtualRelationship(node, n, relationshipType))
109-
.map(r -> new VirtualPath.Builder(r.getStartNode()).push(r).build())
112+
.map(n -> getVirtualRelationship(node, n, direction, relationshipType))
113+
.map(r -> {
114+
VirtualPath virtualPath = new VirtualPath(r.getStartNode());
115+
virtualPath.addRel(r);
116+
return virtualPath;
117+
})
110118
.map(PathResult::new);
111119
}
120+
121+
private VirtualRelationship getVirtualRelationship(
122+
Node node, Node n, String direction, RelationshipType relationshipType) {
123+
if (Objects.equals(direction.toUpperCase(), Direction.OUT.name())) {
124+
return new VirtualRelationship(node, n, relationshipType);
125+
}
126+
return new VirtualRelationship(n, node, relationshipType);
127+
}
128+
129+
enum Direction {
130+
IN,
131+
OUT;
132+
}
112133
}

0 commit comments

Comments
 (0)