Class DataFetcherResult<T>
- java.lang.Object
-
- graphql.execution.DataFetcherResult<T>
-
- Type Parameters:
T- The type of the data fetched
@PublicApi public class DataFetcherResult<T> extends java.lang.Object
An object that can be returned from aDataFetcherthat contains both data, local context and errors to be added to the final result. This is a useful when your ``DataFetcher`` retrieves data from multiple sources or from another GraphQL resource, or you want to pass extra context to lower levels.This also allows you to pass down new local context objects between parent and child fields. If you return a
getLocalContext()value then it will be passed down into any child fields viaDataFetchingEnvironment.getLocalContext()You can also have
DataFetchers contribute to theExecutionResult.getExtensions()by returning extensions maps that will be merged together via theExtensionsBuilderand itsExtensionsMergerin place.This provides
hashCode()andequals(Object)methods that afford comparison with otherDataFetcherResultobject.s However, to function correctly, this relies on the values provided in the following fields in turn also implementinghashCode()} andequals(Object)as appropriate:- The data returned in
getData(). - The individual errors returned in
getErrors(). - The context returned in
getLocalContext(). - The keys/values in the
getExtensions()Map.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classDataFetcherResult.Builder<T>
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(java.lang.Object o)TgetData()java.util.List<GraphQLError>getErrors()java.util.Map<java.lang.Object,java.lang.Object>getExtensions()A data fetcher result can supply extension values that will be merged into the result via theExtensionsBuilderat the end of the operation.java.lang.ObjectgetLocalContext()A data fetcher result can supply a context object for that field that is passed down to child fieldsbooleanhasErrors()inthashCode()<R> DataFetcherResult<R>map(java.util.function.Function<T,R> transformation)Transforms the data of the current DataFetcherResult using the provided function.static <T> DataFetcherResult.Builder<T>newResult()Creates a new data fetcher result builderjava.lang.StringtoString()DataFetcherResult<T>transform(java.util.function.Consumer<DataFetcherResult.Builder<T>> builderConsumer)This helps you transform the current DataFetcherResult into another one by starting a builder with all the current values and allows you to transform it how you want.
-
-
-
Method Detail
-
getData
public T getData()
- Returns:
- The data fetched. May be null.
-
getErrors
public java.util.List<GraphQLError> getErrors()
- Returns:
- errors encountered when fetching data. This will be non null but possibly empty.
-
hasErrors
public boolean hasErrors()
- Returns:
- true if there are any errors present
-
getLocalContext
public java.lang.Object getLocalContext()
A data fetcher result can supply a context object for that field that is passed down to child fields- Returns:
- a local context object
-
getExtensions
public java.util.Map<java.lang.Object,java.lang.Object> getExtensions()
A data fetcher result can supply extension values that will be merged into the result via theExtensionsBuilderat the end of the operation.The
ExtensionsMergerin place inside theExtensionsBuilderwill control how these extension values get merged.- Returns:
- a map of extension values to be merged
- See Also:
ExtensionsBuilder,ExtensionsMerger
-
transform
public DataFetcherResult<T> transform(java.util.function.Consumer<DataFetcherResult.Builder<T>> builderConsumer)
This helps you transform the current DataFetcherResult into another one by starting a builder with all the current values and allows you to transform it how you want.- Parameters:
builderConsumer- the consumer code that will be given a builder to transform- Returns:
- a new instance produced by calling
buildon that builder
-
map
public <R> DataFetcherResult<R> map(java.util.function.Function<T,R> transformation)
Transforms the data of the current DataFetcherResult using the provided function. All other values are left unmodified.- Type Parameters:
R- the result type- Parameters:
transformation- the transformation that should be applied to the data- Returns:
- a new instance with where the data value has been transformed
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
newResult
public static <T> DataFetcherResult.Builder<T> newResult()
Creates a new data fetcher result builder- Type Parameters:
T- the type of the result- Returns:
- a new builder
-
-