Skip to content

Commit 636f7b5

Browse files
authored
result node adaptor now can delete children (#1489)
1 parent 800ea0d commit 636f7b5

7 files changed

Lines changed: 115 additions & 30 deletions

File tree

‎src/main/java/graphql/execution/FetchedValue.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ public FetchedValue transform(Consumer<Builder> builderConsumer) {
4646
return builder.build();
4747
}
4848

49+
@Override
50+
public String toString() {
51+
return "FetchedValue{" +
52+
"fetchedValue=" + fetchedValue +
53+
", rawFetchedValue=" + rawFetchedValue +
54+
", localContext=" + localContext +
55+
", errors=" + errors +
56+
'}';
57+
}
58+
4959
public static Builder newFetchedValue() {
5060
return new Builder();
5161
}

‎src/main/java/graphql/execution/nextgen/FetchedValueAnalysis.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,20 @@ public String getResultKey() {
107107
return executionStepInfo.getResultKey();
108108
}
109109

110+
@Override
111+
public String toString() {
112+
return "{" +
113+
"valueType=" + valueType +
114+
", completedValue=" + completedValue +
115+
", errors=" + errors +
116+
", children=" + children +
117+
", stepInfo=" + executionStepInfo +
118+
", nullValue=" + nullValue +
119+
", resolvedType=" + resolvedType +
120+
", fetchedValue=" + fetchedValue +
121+
'}';
122+
}
123+
110124
public static final class Builder {
111125
private FetchedValueType valueType;
112126
private final List<GraphQLError> errors = new ArrayList<>();

‎src/main/java/graphql/execution/nextgen/result/ExecutionResultNode.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,15 @@ public Optional<NonNullableFieldWasNullException> getChildNonNullableException()
8888
* @return a new ExecutionResultNode with the new errors
8989
*/
9090
public abstract ExecutionResultNode withNewErrors(List<GraphQLError> errors);
91+
92+
93+
@Override
94+
public String toString() {
95+
return getClass().getSimpleName() + "{" +
96+
"fva=" + fetchedValueAnalysis +
97+
", children=" + children +
98+
", errors=" + errors +
99+
", nonNullableEx=" + nonNullableFieldWasNullException +
100+
'}';
101+
}
91102
}
Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package graphql.execution.nextgen.result;
22

3-
import graphql.Assert;
43
import graphql.PublicApi;
54
import graphql.util.NodeAdapter;
65
import graphql.util.NodeLocation;
@@ -9,6 +8,9 @@
98
import java.util.List;
109
import java.util.Map;
1110

11+
import static graphql.Assert.assertNotNull;
12+
import static graphql.Assert.assertTrue;
13+
1214
@PublicApi
1315
public class ResultNodeAdapter implements NodeAdapter<ExecutionResultNode> {
1416

@@ -19,22 +21,26 @@ private ResultNodeAdapter() {
1921
}
2022

2123
@Override
22-
public Map<String, List<ExecutionResultNode>> getNamedChildren(ExecutionResultNode node) {
24+
public Map<String, List<ExecutionResultNode>> getNamedChildren(ExecutionResultNode parentNode) {
2325
Map<String, List<ExecutionResultNode>> result = new LinkedHashMap<>();
24-
result.put(null, node.getChildren());
26+
result.put(null, parentNode.getChildren());
2527
return result;
2628
}
2729

2830
@Override
29-
public ExecutionResultNode withNewChildren(ExecutionResultNode node, Map<String, List<ExecutionResultNode>> newChildren) {
30-
Assert.assertTrue(newChildren.size() == 1);
31+
public ExecutionResultNode withNewChildren(ExecutionResultNode parentNode, Map<String, List<ExecutionResultNode>> newChildren) {
32+
assertTrue(newChildren.size() == 1);
3133
List<ExecutionResultNode> childrenList = newChildren.get(null);
32-
Assert.assertNotNull(childrenList);
33-
return node.withNewChildren(childrenList);
34+
assertNotNull(childrenList);
35+
return parentNode.withNewChildren(childrenList);
3436
}
3537

3638
@Override
37-
public ExecutionResultNode removeChild(ExecutionResultNode node, NodeLocation location) {
38-
throw new UnsupportedOperationException();
39+
public ExecutionResultNode removeChild(ExecutionResultNode parentNode, NodeLocation location) {
40+
int index = location.getIndex();
41+
List<ExecutionResultNode> childrenList = parentNode.getChildren();
42+
assertTrue(index >= 0 && index < childrenList.size(), "The remove index MUST be within the range of the children");
43+
childrenList.remove(index);
44+
return parentNode.withNewChildren(childrenList);
3945
}
4046
}

‎src/test/groovy/graphql/execution/nextgen/result/ExecutionResultNodeTest.groovy‎

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,24 @@
11
package graphql.execution.nextgen.result
22

3-
import graphql.Scalars
4-
import graphql.execution.nextgen.FetchedValueAnalysis
3+
54
import spock.lang.Shared
65
import spock.lang.Specification
76
import spock.lang.Unroll
87

98
import static graphql.GraphqlErrorBuilder.newError
10-
import static graphql.execution.ExecutionStepInfo.newExecutionStepInfo
11-
import static graphql.execution.FetchedValue.newFetchedValue
12-
import static graphql.execution.nextgen.FetchedValueAnalysis.FetchedValueType.SCALAR
13-
import static graphql.execution.nextgen.FetchedValueAnalysis.newFetchedValueAnalysis
9+
import static graphql.execution.nextgen.result.ExecutionResultNodeTestUtils.fvaForValue
1410

1511
class ExecutionResultNodeTest extends Specification {
1612

1713
@Shared
1814
def startingErrors = [newError().message("Starting").build()]
1915

2016
@Shared
21-
def startingFetchValueAnalysis = fetchedValueAnalysis("Starting")
17+
def startingFetchValueAnalysis = fvaForValue("Starting")
2218

2319
@Shared
2420
def startingChildren = [new LeafExecutionResultNode(startingFetchValueAnalysis, null)]
2521

26-
static FetchedValueAnalysis fetchedValueAnalysis(String value) {
27-
def executionStepInfo = newExecutionStepInfo().type(Scalars.GraphQLString).build()
28-
def fetchedValue = newFetchedValue()
29-
.fetchedValue(value)
30-
.rawFetchedValue(value).build()
31-
newFetchedValueAnalysis()
32-
.executionStepInfo(executionStepInfo)
33-
.valueType(SCALAR)
34-
.fetchedValue(fetchedValue)
35-
.completedValue(value)
36-
.build()
37-
}
38-
3922
@Unroll
4023
def "construction of objects with new errors works"() {
4124

@@ -61,7 +44,7 @@ class ExecutionResultNodeTest extends Specification {
6144
def "construction of objects with new fetched value analysis works"() {
6245

6346
given:
64-
def newFetchValueAnalysis = fetchedValueAnalysis("hi")
47+
def newFetchValueAnalysis = fvaForValue("hi")
6548

6649
expect:
6750
ExecutionResultNode nodeUnderTest = node
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package graphql.execution.nextgen.result
2+
3+
import graphql.Scalars
4+
import graphql.execution.nextgen.FetchedValueAnalysis
5+
6+
import static graphql.execution.ExecutionStepInfo.newExecutionStepInfo
7+
import static graphql.execution.FetchedValue.newFetchedValue
8+
import static graphql.execution.nextgen.FetchedValueAnalysis.FetchedValueType.SCALAR
9+
import static graphql.execution.nextgen.FetchedValueAnalysis.newFetchedValueAnalysis
10+
11+
class ExecutionResultNodeTestUtils {
12+
13+
static FetchedValueAnalysis fvaForValue(Object value) {
14+
def executionStepInfo = newExecutionStepInfo().type(Scalars.GraphQLString).build()
15+
def fetchedValue = newFetchedValue()
16+
.fetchedValue(value)
17+
.rawFetchedValue(value).build()
18+
newFetchedValueAnalysis()
19+
.executionStepInfo(executionStepInfo)
20+
.valueType(SCALAR)
21+
.fetchedValue(fetchedValue)
22+
.completedValue(value)
23+
.build()
24+
}
25+
26+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package graphql.execution.nextgen.result
2+
3+
import graphql.AssertException
4+
import graphql.util.NodeLocation
5+
import spock.lang.Specification
6+
7+
import static graphql.execution.nextgen.result.ExecutionResultNodeTestUtils.fvaForValue
8+
9+
class ResultNodeAdapterTest extends Specification {
10+
11+
def parentNode = new ObjectExecutionResultNode(null, [
12+
new LeafExecutionResultNode(fvaForValue("v1"), null),
13+
new LeafExecutionResultNode(fvaForValue("v2"), null),
14+
new LeafExecutionResultNode(fvaForValue("v3"), null),
15+
])
16+
17+
def "can remove a child from a node"() {
18+
when:
19+
ResultNodeAdapter.RESULT_NODE_ADAPTER.removeChild(parentNode, new NodeLocation(null, -1))
20+
then:
21+
thrown(AssertException)
22+
23+
when:
24+
ResultNodeAdapter.RESULT_NODE_ADAPTER.removeChild(parentNode, new NodeLocation(null, -3))
25+
then:
26+
thrown(AssertException)
27+
28+
when:
29+
def newNode = ResultNodeAdapter.RESULT_NODE_ADAPTER.removeChild(parentNode, new NodeLocation(null, 1))
30+
then:
31+
newNode.children.size() == 2
32+
newNode.children[0].getFetchedValueAnalysis().getCompletedValue() == "v1"
33+
newNode.children[1].getFetchedValueAnalysis().getCompletedValue() == "v3"
34+
}
35+
}

0 commit comments

Comments
 (0)