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
22 changes: 22 additions & 0 deletions src/main/java/graphql/ExecutionResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,34 @@


import java.util.List;
import java.util.Map;

/**
* This simple value class represents the result of performing a graphql query.
*/
@PublicApi
public interface ExecutionResult {

/**
* @param <T> allows type coercion
*
* @return the data in the result or null if there is none
*/
<T> T getData();

/**
* @return the errors that occurred during execution or empty list if there is none
*/
List<GraphQLError> getErrors();

/**
* The graphql specification says that result of a call should be a map that follows certain rules on what items
* should be present. Certain JSON serializers may or may interpret {@link ExecutionResult} to spec, so this method
* is provided to produce a map that strictly follows the specification.
*
* See : <a href="http://facebook.github.io/graphql/#sec-Response-Format">http://facebook.github.io/graphql/#sec-Response-Format">http://facebook.github.io/graphql/#sec-Response-Format</a>
*
* @return a map of the result that strictly follows the spec
*/
Map<String, Object> toSpecification();
}
31 changes: 23 additions & 8 deletions src/main/java/graphql/ExecutionResultImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,36 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Internal
public class ExecutionResultImpl implements ExecutionResult {

private final List<GraphQLError> errors = new ArrayList<>();
private Object data;
private final Object data;
private final transient boolean dataPresent;

public ExecutionResultImpl(GraphQLError error) {
this(Collections.singletonList(error));
this(false, null, Collections.singletonList(error));
}

public ExecutionResultImpl(List<? extends GraphQLError> errors) {
this(null, errors);
this(false, null, errors);
}

public ExecutionResultImpl(Object data, List<? extends GraphQLError> errors) {
this(true, data, errors);
}

private ExecutionResultImpl(boolean dataPresent, Object data, List<? extends GraphQLError> errors) {
this.dataPresent = dataPresent;
this.data = data;

if (errors != null && !errors.isEmpty()) {
this.errors.addAll(errors);
}

}

@Override
Expand All @@ -34,13 +41,21 @@ public <T> T getData() {
return (T) data;
}

public void setData(Object result) {
this.data = result;
}

@Override
public List<GraphQLError> getErrors() {
return new ArrayList<>(errors);
}

@Override
public Map<String, Object> toSpecification() {
Map<String, Object> result = new LinkedHashMap<>();
if (dataPresent) {
result.put("data", data);
}
if (errors != null && !errors.isEmpty()) {
result.put("errors", errors);
}
return result;
}

}
2 changes: 1 addition & 1 deletion src/test/groovy/example/http/HttpMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private void handleStarWars(HttpServletRequest httpRequest, HttpServletResponse
private void returnAsJson(HttpServletResponse response, ExecutionResult executionResult) throws IOException {
response.setContentType("application/json");
response.setStatus(HttpServletResponse.SC_OK);
JsonKit.toJson(response, executionResult);
JsonKit.toJson(response, executionResult.toSpecification());
}

private GraphQLSchema buildStarWarsSchema() {
Expand Down
5 changes: 2 additions & 3 deletions src/test/groovy/example/http/JsonKit.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import graphql.ExecutionResult;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
Expand All @@ -21,8 +20,8 @@ public class JsonKit {
.serializeNulls()
.create();

public static void toJson(HttpServletResponse response, ExecutionResult executionResult) throws IOException {
GSON.toJson(executionResult, response.getWriter());
public static void toJson(HttpServletResponse response, Object result) throws IOException {
GSON.toJson(result, response.getWriter());
}

public static Map<String, Object> toMap(String jsonStr) {
Expand Down
62 changes: 62 additions & 0 deletions src/test/groovy/graphql/ExecutionResultTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package graphql

import graphql.language.SourceLocation
import spock.lang.Specification

class ExecutionResultTest extends Specification {

def KNOWN_ERRORS = [new InvalidSyntaxError(new SourceLocation(666,664))]


def "data with no errors"() {
given:
def er = new ExecutionResultImpl("hello world", null)
when:
def actual = er.getData()
def errors = er.getErrors()
def specMap = er.toSpecification()
then:
actual == "hello world"

errors.size() == 0

specMap.size() == 1
specMap["data"] == "hello world"
}

def "errors and data"() {
given:
def er = new ExecutionResultImpl("hello world", KNOWN_ERRORS)
when:
def actual = er.getData()
def errors = er.getErrors()
def specMap = er.toSpecification()
then:
actual == "hello world"

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

specMap.size() == 2
specMap["data"] == "hello world"
specMap["errors"] == KNOWN_ERRORS
}

def "errors and no data"() {
given:
def er = new ExecutionResultImpl(KNOWN_ERRORS)
when:
def actual = er.getData()
def errors = er.getErrors()
def specMap = er.toSpecification()
then:
actual == null

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

specMap.size() == 1
specMap["errors"] == KNOWN_ERRORS
}

}