Skip to content

Commit bd830f8

Browse files
committed
javadoc
1 parent e98c5d4 commit bd830f8

File tree

3 files changed

+73
-14
lines changed

3 files changed

+73
-14
lines changed

src/main/java/graphql/analysis/QueryReducer.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,26 @@
22

33
import graphql.PublicApi;
44

5+
/**
6+
* Used by {@link QueryTraversal} to reduce the fields of a Document (or part of it) to a single value.
7+
* <p/>
8+
* How this happens in detail (pre vs post-order for example) is defined by {@link QueryTraversal}.
9+
* <p/>
10+
* See {@link QueryTraversal#reducePostOrder(QueryReducer, Object)} and {@link QueryTraversal#reducePreOrder(QueryReducer, Object)}
11+
*
12+
* @param <T>
13+
*/
514
@PublicApi
615
@FunctionalInterface
716
public interface QueryReducer<T> {
817

9-
T reduceField(QueryVisitorFieldEnvironment reducerEnvironment, T acc);
18+
/**
19+
* Called each time a field is visited.
20+
*
21+
* @param fieldEnvironment
22+
* @param acc the previous result
23+
*
24+
* @return the new result
25+
*/
26+
T reduceField(QueryVisitorFieldEnvironment fieldEnvironment, T acc);
1027
}

src/main/java/graphql/analysis/QueryTraversal.java

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@
3636
import static graphql.Assert.assertShouldNeverHappen;
3737
import 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
4049
public 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
}

src/main/java/graphql/analysis/QueryVisitor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
import graphql.PublicApi;
44

5+
/**
6+
* User by {@link QueryTraversal} to visit the nodes of a Query.
7+
* <p/>
8+
* How this happens in detail (pre vs post-order for example) is defined by {@link QueryTraversal}.
9+
*/
510
@PublicApi
611
public interface QueryVisitor {
712

0 commit comments

Comments
 (0)