From bc7e739cc179b170f4f06b6a99da53f2007809c3 Mon Sep 17 00:00:00 2001 From: Jordie Biemold Date: Fri, 20 Feb 2026 11:42:01 +0100 Subject: [PATCH] Add generic DataFetcherResult.newBuilder(T data) method --- .../graphql/execution/DataFetcherResult.java | 13 ++++++++++++ .../execution/DataFetcherResultTest.groovy | 21 +++++++++++++++++++ 2 files changed, 34 insertions(+) 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()