Skip to content

Commit a8a273f

Browse files
fcristovaobbakerman
authored andcommitted
Expose if data is present in ExecutionResult (graphql-java#1491)
* Add isDataPresent to ExecutionResult * This allows to follow the GraphQL specification https://graphql.github.io/graphql-spec/June2018/#sec-Data on when to return the data in the response * Remove unnecessary class casts
1 parent 636f7b5 commit a8a273f

6 files changed

Lines changed: 47 additions & 5 deletions

File tree

src/main/java/graphql/ExecutionResult.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@ public interface ExecutionResult {
2323
*/
2424
<T> T getData();
2525

26+
/**
27+
* The graphql specification specifies:
28+
*
29+
* "If an error was encountered before execution begins, the data entry should not be present in the result.
30+
* If an error was encountered during the execution that prevented a valid response, the data entry in the response should be null."
31+
*
32+
* This allows to distinguish between the cases where {@link #getData()} returns null.
33+
*
34+
* See : <a href="https://graphql.github.io/graphql-spec/June2018/#sec-Data">https://graphql.github.io/graphql-spec/June2018/#sec-Data</a>
35+
*
36+
* @return <code>true</code> if the entry "data" should be present in the result
37+
* <code>false</code> otherwise
38+
*/
39+
boolean isDataPresent();
40+
2641
/**
2742
* @return a map of extensions or null if there are none
2843
*/

src/main/java/graphql/ExecutionResultImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public static class Builder {
113113
private List<GraphQLError> errors = new ArrayList<>();
114114
private Map<Object, Object> extensions;
115115

116-
public Builder from(ExecutionResultImpl executionResult) {
116+
public Builder from(ExecutionResult executionResult) {
117117
dataPresent = executionResult.isDataPresent();
118118
data = executionResult.getData();
119119
errors = new ArrayList<>(executionResult.getErrors());

src/main/java/graphql/cachecontrol/CacheControl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public ExecutionResult addTo(ExecutionResult executionResult) {
179179

180180
putHintsInExtensionsMap(currentExtensions);
181181

182-
return ExecutionResultImpl.newExecutionResult().from((ExecutionResultImpl) executionResult)
182+
return ExecutionResultImpl.newExecutionResult().from(executionResult)
183183
.extensions(currentExtensions).build();
184184
}
185185

src/main/java/graphql/execution/Execution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ private CompletableFuture<ExecutionResult> deferSupport(ExecutionContext executi
187187
// we start the rest of the query now to maximize throughput. We have the initial important results
188188
// and now we can start the rest of the calls as early as possible (even before some one subscribes)
189189
Publisher<ExecutionResult> publisher = deferSupport.startDeferredCalls();
190-
return ExecutionResultImpl.newExecutionResult().from((ExecutionResultImpl) er)
190+
return ExecutionResultImpl.newExecutionResult().from(er)
191191
.addExtension(GraphQL.DEFERRED_RESULTS, publisher)
192192
.build();
193193
}

src/main/java/graphql/execution/defer/DeferredCall.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ private ExecutionResult addErrorsEncountered(ExecutionResult executionResult) {
3333
if (errorsEncountered.isEmpty()) {
3434
return executionResult;
3535
}
36-
ExecutionResultImpl sourceResult = (ExecutionResultImpl) executionResult;
37-
ExecutionResultImpl.Builder builder = ExecutionResultImpl.newExecutionResult().from(sourceResult);
36+
ExecutionResultImpl.Builder builder = ExecutionResultImpl.newExecutionResult().from(executionResult);
3837
builder.addErrors(errorsEncountered);
3938
return builder.build();
4039
}

src/test/groovy/graphql/ExecutionResultImplTest.groovy

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class ExecutionResultImplTest extends Specification {
1717
def errors = er.getErrors()
1818
def specMap = er.toSpecification()
1919
then:
20+
er.isDataPresent()
2021
actual == "hello world"
2122

2223
errors.size() == 0
@@ -33,6 +34,7 @@ class ExecutionResultImplTest extends Specification {
3334
def errors = er.getErrors()
3435
def specMap = er.toSpecification()
3536
then:
37+
er.isDataPresent()
3638
actual == "hello world"
3739

3840
errors.size() == 1
@@ -43,6 +45,9 @@ class ExecutionResultImplTest extends Specification {
4345
specMap["errors"] == EXPECTED_SPEC_ERRORS
4446
}
4547

48+
// According to https://graphql.github.io/graphql-spec/June2018/#sec-Data,
49+
// there's a disctinction between `null` data, and no data at all.
50+
// See test below
4651
def "errors and no data"() {
4752
given:
4853
def er = new ExecutionResultImpl(KNOWN_ERRORS)
@@ -51,6 +56,7 @@ class ExecutionResultImplTest extends Specification {
5156
def errors = er.getErrors()
5257
def specMap = er.toSpecification()
5358
then:
59+
!er.isDataPresent()
5460
actual == null
5561

5662
errors.size() == 1
@@ -60,6 +66,28 @@ class ExecutionResultImplTest extends Specification {
6066
specMap["errors"] == EXPECTED_SPEC_ERRORS
6167
}
6268

69+
// According to https://graphql.github.io/graphql-spec/June2018/#sec-Data,
70+
// there's a disctinction between `null` data, and no data at all.
71+
// See test above
72+
def "errors and (present) null data"() {
73+
given:
74+
def er = new ExecutionResultImpl(null, KNOWN_ERRORS)
75+
when:
76+
def actual = er.getData()
77+
def errors = er.getErrors()
78+
def specMap = er.toSpecification()
79+
then:
80+
er.isDataPresent()
81+
actual == null
82+
83+
errors.size() == 1
84+
errors == KNOWN_ERRORS
85+
86+
specMap.size() == 2
87+
specMap["errors"] == EXPECTED_SPEC_ERRORS
88+
specMap["data"] == null
89+
}
90+
6391
def "can have extensions"() {
6492
6593
given:

0 commit comments

Comments
 (0)