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
51 changes: 39 additions & 12 deletions src/main/java/graphql/language/AstTransformer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import graphql.util.TreeParallelTransformer;
import graphql.util.TreeTransformer;

import java.util.Map;
import java.util.concurrent.ForkJoinPool;

import static graphql.Assert.assertNotNull;
Expand All @@ -20,25 +21,38 @@
@PublicApi
public class AstTransformer {


/**
* Transforms the input tree using the Visitor Pattern.
* @param root the root node of the input tree.
* @param nodeVisitor the visitor which will transform the input tree.
* @return the transformed tree.
*/
public Node transform(Node root, NodeVisitor nodeVisitor) {
assertNotNull(root);
assertNotNull(nodeVisitor);

TraverserVisitor<Node> traverserVisitor = new TraverserVisitor<Node>() {
@Override
public TraversalControl enter(TraverserContext<Node> context) {
return context.thisNode().accept(context, nodeVisitor);
}
TraverserVisitor<Node> traverserVisitor = getNodeTraverserVisitor(nodeVisitor);
TreeTransformer<Node> treeTransformer = new TreeTransformer<>(AST_NODE_ADAPTER);
return treeTransformer.transform(root, traverserVisitor);
}

@Override
public TraversalControl leave(TraverserContext<Node> context) {
return TraversalControl.CONTINUE;
}
};
/**
* Transforms the input tree using the Visitor Pattern.
* @param root the root node of the input tree.
* @param nodeVisitor the visitor which will transform the input tree.
* @param rootVars a context argument to pass information into the nodeVisitor. Pass a contextual
* object to your visitor by adding it to this map such that such that the key
* is the class of the object, and the value is the object itself. The object
* can be retrieved within the visitor by calling context.getVarFromParents().
* @return the transformed tree.
*/
public Node transform(Node root, NodeVisitor nodeVisitor, Map<Class<?>, Object> rootVars) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know the previous code did not have JavaDoc but can you please fix this up.

We now have a map of variables and hence we should explain them and how one might get access to them

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Certainly, I took a stab at writing the JavaDocs for the methods I understand. I am not sure how the parallel methods operate so I did not write JavaDocs for them.

assertNotNull(root);
assertNotNull(nodeVisitor);

TraverserVisitor<Node> traverserVisitor = getNodeTraverserVisitor(nodeVisitor);
TreeTransformer<Node> treeTransformer = new TreeTransformer<>(AST_NODE_ADAPTER);
return treeTransformer.transform(root, traverserVisitor);
return treeTransformer.transform(root, traverserVisitor, rootVars);
}

public Node transformParallel(Node root, NodeVisitor nodeVisitor) {
Expand All @@ -61,4 +75,17 @@ public TraversalControl enter(TraverserContext<Node> context) {
return treeParallelTransformer.transform(root, traverserVisitor);
}

private TraverserVisitor<Node> getNodeTraverserVisitor(NodeVisitor nodeVisitor) {
return new TraverserVisitor<Node>() {
@Override
public TraversalControl enter(TraverserContext<Node> context) {
return context.thisNode().accept(context, nodeVisitor);
}

@Override
public TraversalControl leave(TraverserContext<Node> context) {
return TraversalControl.CONTINUE;
}
};
}
}
29 changes: 29 additions & 0 deletions src/test/groovy/graphql/language/AstTransformerTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,35 @@ class AstTransformerTest extends Specification {

}

def "delete node through context"() {
def document = TestUtil.parseQuery("{root { a(arg: 1) { x y } toDelete { x y } } }")

final Map<Class<?>, Object> rootVars = new LinkedHashMap<>();
rootVars.put(String.class, "toDelete");

AstTransformer astTransformer = new AstTransformer()

def visitor = new NodeVisitorStub() {

@Override
TraversalControl visitField(Field field, TraverserContext<Node> context) {
final String fieldToDelete = context.getVarFromParents(String.class);
if (field.name == fieldToDelete) {
return deleteNode(context);
} else {
return TraversalControl.CONTINUE;
}
}
}

when:
def newDocument = astTransformer.transform(document, visitor, rootVars)

then:
printAstCompact(newDocument) == "query {root {a(arg:1) {x y}}}"

}

def "delete node parallel"() {
def document = TestUtil.parseQuery("{root { a(arg: 1) { x y } toDelete { x y } } }")

Expand Down