graphql.schema.TypeTraverser Maven / Gradle / Ivy
package graphql.schema;
import graphql.PublicApi;
import graphql.util.TraversalControl;
import graphql.util.Traverser;
import graphql.util.TraverserContext;
import graphql.util.TraverserResult;
import graphql.util.TraverserVisitor;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import static graphql.util.TraversalControl.CONTINUE;
@PublicApi
public class TypeTraverser {
private final Function super GraphQLType, ? extends List> getChildren;
public TypeTraverser(Function super GraphQLType, ? extends List> getChildren) {
this.getChildren = getChildren;
}
public TypeTraverser() {
this(GraphQLType::getChildren);
}
public TraverserResult depthFirst(GraphQLTypeVisitor graphQLTypeVisitor, GraphQLType root) {
return depthFirst(graphQLTypeVisitor, Collections.singletonList(root));
}
public TraverserResult depthFirst(final GraphQLTypeVisitor graphQLTypeVisitor, Collection extends GraphQLType> roots) {
return depthFirst(initTraverser(), new TraverserDelegateVisitor(graphQLTypeVisitor), roots);
}
public TraverserResult depthFirst(final GraphQLTypeVisitor graphQLTypeVisitor,
Collection extends GraphQLType> roots,
Map types) {
return depthFirst(initTraverser().rootVar(TypeTraverser.class, types), new TraverserDelegateVisitor(graphQLTypeVisitor), roots);
}
public TraverserResult depthFirst(final Traverser traverser,
final TraverserDelegateVisitor traverserDelegateVisitor,
Collection extends GraphQLType> roots) {
return doTraverse(traverser, roots, traverserDelegateVisitor);
}
private Traverser initTraverser() {
return Traverser.depthFirst(getChildren);
}
private TraverserResult doTraverse(Traverser traverser, Collection extends GraphQLType> roots, TraverserDelegateVisitor traverserDelegateVisitor) {
return traverser.traverse(roots, traverserDelegateVisitor);
}
private static class TraverserDelegateVisitor implements TraverserVisitor {
private final GraphQLTypeVisitor before;
TraverserDelegateVisitor(GraphQLTypeVisitor delegate) {
this.before = delegate;
}
@Override
public TraversalControl enter(TraverserContext context) {
return context.thisNode().accept(context, before);
}
@Override
public TraversalControl leave(TraverserContext context) {
return CONTINUE;
}
@Override
public TraversalControl backRef(TraverserContext context) {
return before.visitBackRef(context);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy