Skip to content

Commit bc7e739

Browse files
committed
Add generic DataFetcherResult.newBuilder(T data) method
1 parent 9ae83ab commit bc7e739

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

src/main/java/graphql/execution/DataFetcherResult.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,19 @@ public static <T> Builder<T> newResult() {
175175
return new Builder<>();
176176
}
177177

178+
/**
179+
* Creates a new data fetcher result builder with associated data.
180+
* <p>Data may later be overwritten using {@link Builder#data(Object)}.
181+
*
182+
* @param data the data
183+
* @param <T> the type of the result
184+
*
185+
* @return a new builder
186+
*/
187+
public static <T> Builder<@Nullable T> newResult(@Nullable T data) {
188+
return new Builder<>(data);
189+
}
190+
178191
public static class Builder<T extends @Nullable Object> {
179192
private @Nullable T data;
180193
private @Nullable Object localContext;

src/test/groovy/graphql/execution/DataFetcherResultTest.groovy

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,27 @@ class DataFetcherResultTest extends Specification {
2121
result.getErrors() == [error1, error2]
2222
}
2323

24+
def "building with generics"() {
25+
when:
26+
DataFetcherResult<String> result = DataFetcherResult.newResult("hello")
27+
.error(error1).errors([error2]).localContext("world").build()
28+
then:
29+
result.getData() == "hello"
30+
result.getLocalContext() == "world"
31+
result.getErrors() == [error1, error2]
32+
}
33+
34+
def "building with generics data can be overwritten in builder"() {
35+
when:
36+
DataFetcherResult<String> result = DataFetcherResult.newResult("someText")
37+
.data("hello")
38+
.error(error1).errors([error2]).localContext("world").build()
39+
then:
40+
result.getData() == "hello"
41+
result.getLocalContext() == "world"
42+
result.getErrors() == [error1, error2]
43+
}
44+
2445
def "hasErrors can be called"() {
2546
when:
2647
def builder = DataFetcherResult.newResult()

0 commit comments

Comments
 (0)