diff --git a/src/main/java/graphql/execution/DataFetcherResult.java b/src/main/java/graphql/execution/DataFetcherResult.java index c7a58d84a1..73a9bd1156 100644 --- a/src/main/java/graphql/execution/DataFetcherResult.java +++ b/src/main/java/graphql/execution/DataFetcherResult.java @@ -175,6 +175,19 @@ public static Builder newResult() { return new Builder<>(); } + /** + * Creates a new data fetcher result builder with associated data. + *

Data may later be overwritten using {@link Builder#data(Object)}. + * + * @param data the data + * @param the type of the result + * + * @return a new builder + */ + public static Builder<@Nullable T> newResult(@Nullable T data) { + return new Builder<>(data); + } + public static class Builder { private @Nullable T data; private @Nullable Object localContext; diff --git a/src/test/groovy/graphql/execution/DataFetcherResultTest.groovy b/src/test/groovy/graphql/execution/DataFetcherResultTest.groovy index 47d87b991f..525b17c295 100644 --- a/src/test/groovy/graphql/execution/DataFetcherResultTest.groovy +++ b/src/test/groovy/graphql/execution/DataFetcherResultTest.groovy @@ -21,6 +21,27 @@ class DataFetcherResultTest extends Specification { result.getErrors() == [error1, error2] } + def "building with generics"() { + when: + DataFetcherResult 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 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()