Skip to content

Commit 3833d72

Browse files
committed
improve traverser: add getParentNodes and isRootContext to
TraverserState
1 parent ce19b7c commit 3833d72

5 files changed

Lines changed: 77 additions & 10 deletions

File tree

‎src/main/java/graphql/util/DefaultTraverserContext.java‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import graphql.Internal;
44

5+
import java.util.ArrayList;
6+
import java.util.List;
57
import java.util.Map;
68
import java.util.Set;
79

@@ -18,27 +20,30 @@ public class DefaultTraverserContext<T> implements TraverserContext<T> {
1820
private boolean hasNewAccValue;
1921
private Object curAccValue;
2022
private final NodePosition position;
23+
private final boolean isRootContext;
2124

2225
public DefaultTraverserContext(T curNode,
2326
TraverserContext<T> parent,
2427
Set<T> visited,
2528
Map<Class<?>, Object> vars,
2629
Object sharedContextData,
27-
NodePosition position) {
30+
NodePosition position,
31+
boolean isRootContext) {
2832
this.curNode = curNode;
2933
this.parent = parent;
3034
this.visited = visited;
3135
this.vars = vars;
3236
this.sharedContextData = sharedContextData;
3337
this.position = position;
38+
this.isRootContext = isRootContext;
3439
}
3540

3641
public static <T> DefaultTraverserContext<T> dummy() {
37-
return new DefaultTraverserContext<>(null, null, null, null, null, null);
42+
return new DefaultTraverserContext<>(null, null, null, null, null, null, true);
3843
}
3944

4045
public static <T> DefaultTraverserContext<T> simple(T node) {
41-
return new DefaultTraverserContext<>(node, null, null, null, null, null);
46+
return new DefaultTraverserContext<>(node, null, null, null, null, null, true);
4247
}
4348

4449
@Override
@@ -51,6 +56,17 @@ public TraverserContext<T> getParentContext() {
5156
return parent;
5257
}
5358

59+
@Override
60+
public List<T> getParentNodes() {
61+
List<T> result = new ArrayList<>();
62+
TraverserContext<T> curContext = parent;
63+
while (!curContext.isRootContext()) {
64+
result.add(curContext.thisNode());
65+
curContext = curContext.getParentContext();
66+
}
67+
return result;
68+
}
69+
5470
@Override
5571
public Set<T> visitedNodes() {
5672
return visited;
@@ -110,4 +126,9 @@ void setCurAccValue(Object curAccValue) {
110126
public NodePosition getPosition() {
111127
return position;
112128
}
129+
130+
@Override
131+
public boolean isRootContext() {
132+
return isRootContext;
133+
}
113134
}

‎src/main/java/graphql/util/Traverser.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public TraverserResult traverse(Collection<? extends T> roots, TraverserVisitor<
9696

9797

9898
// "artificial" parent context for all roots with rootVars
99-
DefaultTraverserContext<T> rootContext = traverserState.newContextWithVars(null, null, rootVars, null);
99+
DefaultTraverserContext<T> rootContext = traverserState.newRootContext(rootVars);
100100
traverserState.addNewContexts(roots, rootContext);
101101

102102
DefaultTraverserContext currentContext;

‎src/main/java/graphql/util/TraverserContext.java‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
import graphql.PublicApi;
44

5+
import java.util.List;
56
import java.util.Set;
67

78
/**
89
* Traversal context.
910
*
1011
* It is used as providing context for traversing, but also for returning an accumulate value. ({@link #setAccumulate(Object)}
1112
*
12-
* Parts of the context are mutable.
13+
* There is always a "fake" root context with null node, null parent, null position. See {@link #isRootContext()}
1314
*
1415
* @param <T> type of tree node
1516
*/
@@ -18,7 +19,7 @@ public interface TraverserContext<T> {
1819
/**
1920
* Returns current node being visited
2021
*
21-
* @return current node traverser is visiting
22+
* @return current node traverser is visiting. Is null for the root context
2223
*/
2324
T thisNode();
2425

@@ -33,9 +34,17 @@ public interface TraverserContext<T> {
3334
*/
3435
TraverserContext<T> getParentContext();
3536

37+
/**
38+
* The list of parent nodes starting from the current parent.
39+
*
40+
* @return list of parent nodes
41+
*/
42+
List<T> getParentNodes();
43+
3644
/**
3745
* The position of the current node regarding to the parent node.
38-
* @return the position or null if this is a root node
46+
*
47+
* @return the position or null if this node is a root node
3948
*/
4049
NodePosition getPosition();
4150

@@ -107,4 +116,11 @@ public interface TraverserContext<T> {
107116
*/
108117
<U> U getSharedContextData();
109118

119+
/**
120+
* Returns true for the root context, which doesn't have a node or a position.
121+
*
122+
* @return true for the root context, otherwise false
123+
*/
124+
boolean isRootContext();
125+
110126
}

‎src/main/java/graphql/util/TraverserState.java‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,21 @@ public void addVisited(T visited) {
104104
this.visited.add(visited);
105105
}
106106

107+
108+
public DefaultTraverserContext<T> newRootContext(Map<Class<?>, Object> vars) {
109+
return newContextImpl(null, null, vars, null, true);
110+
}
111+
107112
private DefaultTraverserContext<T> newContext(T o, TraverserContext<T> parent, NodePosition position) {
108-
return newContextWithVars(o, parent, new LinkedHashMap<>(), position);
113+
return newContextImpl(o, parent, new LinkedHashMap<>(), position, false);
109114
}
110115

111-
public DefaultTraverserContext<T> newContextWithVars(T curNode, TraverserContext<T> parent, Map<Class<?>, Object> vars, NodePosition nodePosition) {
116+
private DefaultTraverserContext<T> newContextImpl(T curNode,
117+
TraverserContext<T> parent,
118+
Map<Class<?>, Object> vars,
119+
NodePosition nodePosition,
120+
boolean isRootContext) {
112121
assertNotNull(vars);
113-
return new DefaultTraverserContext<>(curNode, parent, visited, vars, sharedContextData, nodePosition);
122+
return new DefaultTraverserContext<>(curNode, parent, visited, vars, sharedContextData, nodePosition, isRootContext);
114123
}
115124
}

‎src/test/groovy/graphql/util/TraverserTest.groovy‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,27 @@ class TraverserTest extends Specification {
446446
]
447447
}
448448

449+
def "test parent nodes"() {
450+
given:
451+
def visitor = [
452+
enter: { TraverserContext context ->
453+
context.getSharedContextData() << context.getParentNodes().collect { it.number }
454+
TraversalControl.CONTINUE
455+
},
456+
leave: { TraverserContext context ->
457+
TraversalControl.CONTINUE
458+
}
459+
] as TraverserVisitor
460+
def list = []
461+
when:
462+
Traverser.depthFirst({ n -> n.children }, list).traverse(root, visitor)
463+
464+
465+
then:
466+
list == [[], [0], [1, 0], [0], [2, 0], [2, 0]]
467+
468+
}
469+
449470

450471
}
451472

0 commit comments

Comments
 (0)