@Target(value=METHOD)
@Retention(value=RUNTIME)
public @interface Batched
When placed on DataFetcher.get(DataFetchingEnvironment)
, indicates that this DataFetcher is batched.
This annotation must be used in conjunction with BatchedExecutionStrategy
. Batching is valuable in many
situations, such as when a DataFetcher
must make a network or file system request.
When a DataFetcher
is batched, the DataFetchingEnvironment.getSource()
method is
guaranteed to return a List
. The DataFetcher.get(DataFetchingEnvironment)
method MUST return a parallel List
which is equivalent to running a DataFetcher
over each input element individually.
Using the Batched
annotation is equivalent to implementing BatchedDataFetcher
instead of DataFetcher
.
It is preferred to use the Batched
annotation.
DataFetcher
objects are interchangeable if used with a
BatchedExecutionStrategy
.
new DataFetcher() {
@Override
@Batched
public Object get(DataFetchingEnvironment environment) {
List<String> retVal = new ArrayList<>();
for (String s: (List<String>) environment.getSource()) {}
retVal.add(s + environment.getArgument("text"));
return retVal;
}
}
new DataFetcher() {
@Override
public Object get(DataFetchingEnvironment e) {
return ((String)e.getSource()) + e.getArgument("text");
}
}