3636import static graphql .Assert .assertShouldNeverHappen ;
3737import static graphql .language .NodeTraverser .LeaveOrEnter .LEAVE ;
3838
39+ /**
40+ * Helps to traverse (or reduce) a Document (or parts of it) and tracks at the same time the corresponding Schema types.
41+ * <p/>
42+ * This is an important distinction to just traversing the Document without any type information: Each field has a clearly
43+ * defined type. See {@link QueryVisitorFieldEnvironment}.
44+ * <p/>
45+ * Further are the built in Directives skip/include automatically evaluated: if parts of the Document should be ignored they will not
46+ * be visited.
47+ */
3948@ PublicApi
4049public class QueryTraversal {
4150
@@ -76,27 +85,33 @@ public QueryTraversal(GraphQLSchema schema,
7685 this .childrenOfSelectionProvider = new ChildrenOfSelectionProvider (fragmentsByName );
7786 }
7887
88+ /**
89+ * Visits the Document (or parts of it) in post-order.
90+ *
91+ * @param visitor
92+ */
7993 public void visitPostOrder (QueryVisitor visitor ) {
8094 visitImpl (visitor , false );
8195 }
8296
97+ /**
98+ * Visits the Document (or parts of it) in pre-order.
99+ *
100+ * @param visitor
101+ */
83102 public void visitPreOrder (QueryVisitor visitor ) {
84103 visitImpl (visitor , true );
85104 }
86105
87- private GraphQLObjectType getRootTypeFromOperation (OperationDefinition operationDefinition ) {
88- switch (operationDefinition .getOperation ()) {
89- case MUTATION :
90- return assertNotNull (schema .getMutationType ());
91- case QUERY :
92- return assertNotNull (schema .getQueryType ());
93- case SUBSCRIPTION :
94- return assertNotNull (schema .getSubscriptionType ());
95- default :
96- return assertShouldNeverHappen ();
97- }
98- }
99-
106+ /**
107+ * Reduces the fields of a Document (or parts of it) to a single value. The fields are visited in post-order.
108+ *
109+ * @param queryReducer
110+ * @param initialValue
111+ * @param <T>
112+ *
113+ * @return the calculated overall value
114+ */
100115 @ SuppressWarnings ("unchecked" )
101116 public <T > T reducePostOrder (QueryReducer <T > queryReducer , T initialValue ) {
102117 // compiler hack to make acc final and mutable :-)
@@ -110,6 +125,15 @@ public void visitField(QueryVisitorFieldEnvironment env) {
110125 return (T ) acc [0 ];
111126 }
112127
128+ /**
129+ * Reduces the fields of a Document (or parts of it) to a single value. The fields are visited in pre-order.
130+ *
131+ * @param queryReducer
132+ * @param initialValue
133+ * @param <T>
134+ *
135+ * @return the calucalated overall value
136+ */
113137 @ SuppressWarnings ("unchecked" )
114138 public <T > T reducePreOrder (QueryReducer <T > queryReducer , T initialValue ) {
115139 // compiler hack to make acc final and mutable :-)
@@ -123,6 +147,19 @@ public void visitField(QueryVisitorFieldEnvironment env) {
123147 return (T ) acc [0 ];
124148 }
125149
150+ private GraphQLObjectType getRootTypeFromOperation (OperationDefinition operationDefinition ) {
151+ switch (operationDefinition .getOperation ()) {
152+ case MUTATION :
153+ return assertNotNull (schema .getMutationType ());
154+ case QUERY :
155+ return assertNotNull (schema .getQueryType ());
156+ case SUBSCRIPTION :
157+ return assertNotNull (schema .getSubscriptionType ());
158+ default :
159+ return assertShouldNeverHappen ();
160+ }
161+ }
162+
126163 private List <Node > childrenOf (Node selection ) {
127164 return childrenOfSelectionProvider .getSelections ((Selection ) selection );
128165 }
0 commit comments