Package graphql.analysis
Class QueryTraverser
- java.lang.Object
-
- graphql.analysis.QueryTraverser
-
@PublicApi public class QueryTraverser extends java.lang.Object
Helps to traverse (or reduce) a Document (or parts of it) and tracks at the same time the corresponding Schema types.This is an important distinction to just traversing the Document without any type information: Each field has a clearly defined type. See
QueryVisitorFieldEnvironment.Furthermore are the built in Directives skip/include automatically evaluated: if parts of the Document should be ignored they will not be visited. But this is not a full evaluation of a Query: every fragment will be visited/followed regardless of the type condition.
It also doesn't consider field merging, which means for example
{ user{firstName} user{firstName}}will result in four visitField calls.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classQueryTraverser.Builder
-
Field Summary
Fields Modifier and Type Field Description private CoercedVariablescoercedVariablesprivate java.util.Map<java.lang.String,FragmentDefinition>fragmentsByNameprivate GraphQLCompositeTyperootParentTypeprivate java.util.Collection<? extends Node>rootsprivate GraphQLSchemaschema
-
Constructor Summary
Constructors Modifier Constructor Description privateQueryTraverser(GraphQLSchema schema, Document document, java.lang.String operation, CoercedVariables coercedVariables)privateQueryTraverser(GraphQLSchema schema, Document document, java.lang.String operation, RawVariables rawVariables)privateQueryTraverser(GraphQLSchema schema, Node root, GraphQLCompositeType rootParentType, java.util.Map<java.lang.String,FragmentDefinition> fragmentsByName, CoercedVariables coercedVariables)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private java.util.List<Node>childrenOf(Node<?> node)private GraphQLObjectTypegetRootTypeFromOperation(OperationDefinition operationDefinition)static QueryTraverser.BuildernewQueryTraverser()<T> TreducePostOrder(QueryReducer<T> queryReducer, T initialValue)Reduces the fields of a Document (or parts of it) to a single value.<T> TreducePreOrder(QueryReducer<T> queryReducer, T initialValue)Reduces the fields of a Document (or parts of it) to a single value.java.lang.ObjectvisitDepthFirst(QueryVisitor queryVisitor)private java.lang.ObjectvisitImpl(QueryVisitor visitFieldCallback, java.lang.Boolean preOrder)voidvisitPostOrder(QueryVisitor visitor)Visits the Document (or parts of it) in post-order.voidvisitPreOrder(QueryVisitor visitor)Visits the Document (or parts of it) in pre-order.
-
-
-
Field Detail
-
roots
private final java.util.Collection<? extends Node> roots
-
schema
private final GraphQLSchema schema
-
fragmentsByName
private final java.util.Map<java.lang.String,FragmentDefinition> fragmentsByName
-
coercedVariables
private CoercedVariables coercedVariables
-
rootParentType
private final GraphQLCompositeType rootParentType
-
-
Constructor Detail
-
QueryTraverser
private QueryTraverser(GraphQLSchema schema, Document document, java.lang.String operation, CoercedVariables coercedVariables)
-
QueryTraverser
private QueryTraverser(GraphQLSchema schema, Document document, java.lang.String operation, RawVariables rawVariables)
-
QueryTraverser
private QueryTraverser(GraphQLSchema schema, Node root, GraphQLCompositeType rootParentType, java.util.Map<java.lang.String,FragmentDefinition> fragmentsByName, CoercedVariables coercedVariables)
-
-
Method Detail
-
visitDepthFirst
public java.lang.Object visitDepthFirst(QueryVisitor queryVisitor)
-
visitPostOrder
public void visitPostOrder(QueryVisitor visitor)
Visits the Document (or parts of it) in post-order.- Parameters:
visitor- the query visitor that will be called back
-
visitPreOrder
public void visitPreOrder(QueryVisitor visitor)
Visits the Document (or parts of it) in pre-order.- Parameters:
visitor- the query visitor that will be called back
-
reducePostOrder
public <T> T reducePostOrder(QueryReducer<T> queryReducer, T initialValue)
Reduces the fields of a Document (or parts of it) to a single value. The fields are visited in post-order.- Type Parameters:
T- the type of reduced value- Parameters:
queryReducer- the query reducerinitialValue- the initial value to pass to the reducer- Returns:
- the calculated overall value
-
reducePreOrder
public <T> T reducePreOrder(QueryReducer<T> queryReducer, T initialValue)
Reduces the fields of a Document (or parts of it) to a single value. The fields are visited in pre-order.- Type Parameters:
T- the type of reduced value- Parameters:
queryReducer- the query reducerinitialValue- the initial value to pass to the reducer- Returns:
- the calculated overall value
-
getRootTypeFromOperation
private GraphQLObjectType getRootTypeFromOperation(OperationDefinition operationDefinition)
-
visitImpl
private java.lang.Object visitImpl(QueryVisitor visitFieldCallback, java.lang.Boolean preOrder)
-
newQueryTraverser
public static QueryTraverser.Builder newQueryTraverser()
-
-