99
1010import java .util .ArrayList ;
1111import java .util .List ;
12+ import java .util .Map ;
1213import java .util .function .Consumer ;
1314
1415import static graphql .Assert .assertNotNull ;
1819 * An object that can be returned from a {@link DataFetcher} that contains both data, local context and errors to be relativized and
1920 * added to the final result. This is a useful when your ``DataFetcher`` retrieves data from multiple sources
2021 * or from another GraphQL resource or you want to pass extra context to lower levels.
21- *
22+ * <p>
2223 * This also allows you to pass down new local context objects between parent and child fields. If you return a
2324 * {@link #getLocalContext()} value then it will be passed down into any child fields via
2425 * {@link graphql.schema.DataFetchingEnvironment#getLocalContext()}
@@ -31,6 +32,7 @@ public class DataFetcherResult<T> {
3132 private final T data ;
3233 private final List <GraphQLError > errors ;
3334 private final Object localContext ;
35+ private final Map <Object , Object > extensions ;
3436
3537 /**
3638 * Creates a data fetcher result
@@ -44,13 +46,14 @@ public class DataFetcherResult<T> {
4446 @ Deprecated
4547 @ DeprecatedAt ("2019-01-11" )
4648 public DataFetcherResult (T data , List <GraphQLError > errors ) {
47- this (data , errors , null );
49+ this (data , errors , null , null );
4850 }
4951
50- private DataFetcherResult (T data , List <GraphQLError > errors , Object localContext ) {
52+ private DataFetcherResult (T data , List <GraphQLError > errors , Object localContext , Map < Object , Object > extensions ) {
5153 this .data = data ;
5254 this .errors = ImmutableList .copyOf (assertNotNull (errors ));
5355 this .localContext = localContext ;
56+ this .extensions = extensions ;
5457 }
5558
5659 /**
@@ -83,6 +86,22 @@ public Object getLocalContext() {
8386 return localContext ;
8487 }
8588
89+ /**
90+ * A data fetcher result can supply extension values that will be merged into the result
91+ * via the {@link graphql.extensions.ExtensionsBuilder} at the end of the operation.
92+ * <p>
93+ * The {@link graphql.extensions.ExtensionsMerger} in place inside the {@link graphql.extensions.ExtensionsBuilder}
94+ * will control how these extension values get merged.
95+ *
96+ * @return a map of extension values to be merged
97+ *
98+ * @see graphql.extensions.ExtensionsBuilder
99+ * @see graphql.extensions.ExtensionsMerger
100+ */
101+ public Map <Object , Object > getExtensions () {
102+ return extensions ;
103+ }
104+
86105 /**
87106 * This helps you transform the current DataFetcherResult into another one by starting a builder with all
88107 * the current values and allows you to transform it how you want.
@@ -112,11 +131,13 @@ public static class Builder<T> {
112131 private T data ;
113132 private Object localContext ;
114133 private final List <GraphQLError > errors = new ArrayList <>();
134+ private Map <Object , Object > extensions ;
115135
116136 public Builder (DataFetcherResult <T > existing ) {
117137 data = existing .getData ();
118138 localContext = existing .getLocalContext ();
119139 errors .addAll (existing .getErrors ());
140+ extensions = existing .extensions ;
120141 }
121142
122143 public Builder (T data ) {
@@ -158,8 +179,13 @@ public Builder<T> localContext(Object localContext) {
158179 return this ;
159180 }
160181
182+ public Builder <T > extensions (Map <Object , Object > extensions ) {
183+ this .extensions = extensions ;
184+ return this ;
185+ }
186+
161187 public DataFetcherResult <T > build () {
162- return new DataFetcherResult <>(data , errors , localContext );
188+ return new DataFetcherResult <>(data , errors , localContext , extensions );
163189 }
164190 }
165191}
0 commit comments