Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/main/java/graphql/ExecutionResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ public interface ExecutionResult {
*/
<T> T getData();

/**
* The graphql specification specifies:
*
* "If an error was encountered before execution begins, the data entry should not be present in the result.
* If an error was encountered during the execution that prevented a valid response, the data entry in the response should be null."
*
* This allows to distinguish between the cases where {@link #getData()} returns null.
*
* See : <a href="https://graphql.github.io/graphql-spec/June2018/#sec-Data">https://graphql.github.io/graphql-spec/June2018/#sec-Data</a>
*
* @return <code>true</code> if the entry "data" should be present in the result
* <code>false</code> otherwise
*/
boolean isDataPresent();

/**
* @return a map of extensions or null if there are none
*/
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/graphql/ExecutionResultImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public static class Builder {
private List<GraphQLError> errors = new ArrayList<>();
private Map<Object, Object> extensions;

public Builder from(ExecutionResultImpl executionResult) {
public Builder from(ExecutionResult executionResult) {
dataPresent = executionResult.isDataPresent();
data = executionResult.getData();
errors = new ArrayList<>(executionResult.getErrors());
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/graphql/cachecontrol/CacheControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public ExecutionResult addTo(ExecutionResult executionResult) {

putHintsInExtensionsMap(currentExtensions);

return ExecutionResultImpl.newExecutionResult().from((ExecutionResultImpl) executionResult)
return ExecutionResultImpl.newExecutionResult().from(executionResult)
.extensions(currentExtensions).build();
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/graphql/execution/Execution.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ private CompletableFuture<ExecutionResult> deferSupport(ExecutionContext executi
// we start the rest of the query now to maximize throughput. We have the initial important results
// and now we can start the rest of the calls as early as possible (even before some one subscribes)
Publisher<ExecutionResult> publisher = deferSupport.startDeferredCalls();
return ExecutionResultImpl.newExecutionResult().from((ExecutionResultImpl) er)
return ExecutionResultImpl.newExecutionResult().from(er)
.addExtension(GraphQL.DEFERRED_RESULTS, publisher)
.build();
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/graphql/execution/defer/DeferredCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ private ExecutionResult addErrorsEncountered(ExecutionResult executionResult) {
if (errorsEncountered.isEmpty()) {
return executionResult;
}
ExecutionResultImpl sourceResult = (ExecutionResultImpl) executionResult;
ExecutionResultImpl.Builder builder = ExecutionResultImpl.newExecutionResult().from(sourceResult);
ExecutionResultImpl.Builder builder = ExecutionResultImpl.newExecutionResult().from(executionResult);
builder.addErrors(errorsEncountered);
return builder.build();
}
Expand Down
28 changes: 28 additions & 0 deletions src/test/groovy/graphql/ExecutionResultImplTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class ExecutionResultImplTest extends Specification {
def errors = er.getErrors()
def specMap = er.toSpecification()
then:
er.isDataPresent()
actual == "hello world"

errors.size() == 0
Expand All @@ -33,6 +34,7 @@ class ExecutionResultImplTest extends Specification {
def errors = er.getErrors()
def specMap = er.toSpecification()
then:
er.isDataPresent()
actual == "hello world"

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

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

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

// According to https://graphql.github.io/graphql-spec/June2018/#sec-Data,
// there's a disctinction between `null` data, and no data at all.
// See test above
def "errors and (present) null data"() {
given:
def er = new ExecutionResultImpl(null, KNOWN_ERRORS)
when:
def actual = er.getData()
def errors = er.getErrors()
def specMap = er.toSpecification()
then:
er.isDataPresent()
actual == null

errors.size() == 1
errors == KNOWN_ERRORS

specMap.size() == 2
specMap["errors"] == EXPECTED_SPEC_ERRORS
specMap["data"] == null
}

def "can have extensions"() {

given:
Expand Down