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
13 changes: 13 additions & 0 deletions src/main/java/graphql/execution/DataFetcherResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,19 @@ public static <T> Builder<T> newResult() {
return new Builder<>();
}

/**
* Creates a new data fetcher result builder with associated data.
* <p>Data may later be overwritten using {@link Builder#data(Object)}.
*
* @param data the data
* @param <T> the type of the result
*
* @return a new builder
*/
public static <T> Builder<@Nullable T> newResult(@Nullable T data) {
return new Builder<>(data);
}

public static class Builder<T extends @Nullable Object> {
private @Nullable T data;
private @Nullable Object localContext;
Expand Down
21 changes: 21 additions & 0 deletions src/test/groovy/graphql/execution/DataFetcherResultTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@ class DataFetcherResultTest extends Specification {
result.getErrors() == [error1, error2]
}

def "building with generics"() {
when:
DataFetcherResult<String> result = DataFetcherResult.newResult("hello")
.error(error1).errors([error2]).localContext("world").build()
then:
result.getData() == "hello"
result.getLocalContext() == "world"
result.getErrors() == [error1, error2]
}

def "building with generics data can be overwritten in builder"() {
when:
DataFetcherResult<String> result = DataFetcherResult.newResult("someText")
.data("hello")
.error(error1).errors([error2]).localContext("world").build()
then:
result.getData() == "hello"
result.getLocalContext() == "world"
result.getErrors() == [error1, error2]
}

def "hasErrors can be called"() {
when:
def builder = DataFetcherResult.newResult()
Expand Down
Loading