Skip to content

Commit a9fb38b

Browse files
authored
Renamed to Async to be more in line with other AsyncXXX classes (graphql-java#801)
1 parent c59c31a commit a9fb38b

File tree

4 files changed

+58
-54
lines changed

4 files changed

+58
-54
lines changed

docs/execution.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ The graphql-java engine ensures that all the ``CompletableFuture`` objects are c
346346
that follows the graphql specification.
347347

348348
There is a helpful shortcut in graphql-java to create asynchronous data fetchers.
349-
Use ``graphql.schema.AsynchronousDataFetcher.async(DataFetcher<T>)`` to wrap a
349+
Use ``graphql.schema.AsyncDataFetcher.async(DataFetcher<T>)`` to wrap a
350350
``DataFetcher``. This can be used with static imports to produce more readable code.
351351

352352
.. code-block:: java

src/main/java/graphql/schema/AsynchronousDataFetcher.java renamed to src/main/java/graphql/schema/AsyncDataFetcher.java

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,61 @@
11
package graphql.schema;
22

3-
import static graphql.Assert.assertNotNull;
3+
import graphql.PublicApi;
44

55
import java.util.concurrent.CompletableFuture;
66
import java.util.concurrent.Executor;
77
import java.util.concurrent.ForkJoinPool;
88

9-
import graphql.PublicApi;
9+
import static graphql.Assert.assertNotNull;
1010

1111
/**
1212
* A modifier type that indicates the underlying data fetcher is run asynchronously
1313
*/
1414
@PublicApi
15-
public class AsynchronousDataFetcher<T> implements DataFetcher<CompletableFuture<T>> {
16-
15+
public class AsyncDataFetcher<T> implements DataFetcher<CompletableFuture<T>> {
16+
1717
/**
18-
* A factory method for creating asynchronous data fetchers so that when used with
18+
* A factory method for creating asynchronous data fetchers so that when used with
1919
* static imports allows more readable code such as:
2020
* <p>
2121
* {@code .dataFetcher(async(fooDataFetcher))}
2222
* <p>
23-
* By default this will run in the {@link ForkJoinPool#commonPool()}. You can set
24-
* your own {@link Executor} with {@link #asyncWithExecutor(DataFetcher, Executor)}
23+
* By default this will run in the {@link ForkJoinPool#commonPool()}. You can set
24+
* your own {@link Executor} with {@link #async(DataFetcher)} (DataFetcher, Executor)}
2525
*
2626
* @param wrappedDataFetcher the data fetcher to run asynchronously
2727
*
2828
* @return a {@link DataFetcher} that will run the wrappedDataFetcher asynchronously
2929
*/
30-
public static <T> AsynchronousDataFetcher<T> async(DataFetcher<T> wrappedDataFetcher) {
31-
return new AsynchronousDataFetcher<>(wrappedDataFetcher);
30+
public static <T> AsyncDataFetcher<T> async(DataFetcher<T> wrappedDataFetcher) {
31+
return new AsyncDataFetcher<>(wrappedDataFetcher);
3232
}
33-
33+
3434
/**
35-
* A factory method for creating asynchronous data fetchers and setting the
36-
* {@link Executor} they run in so that when used with static imports allows
35+
* A factory method for creating asynchronous data fetchers and setting the
36+
* {@link Executor} they run in so that when used with static imports allows
3737
* more readable code such as:
3838
* <p>
39-
* {@code .dataFetcher(asyncWithExecutor(fooDataFetcher, fooPool))}
39+
* {@code .dataFetcher(async(fooDataFetcher, fooExecutor))}
4040
*
4141
* @param wrappedDataFetcher the data fetcher to run asynchronously
42-
* @param executor to run the asynchronous data fetcher in
42+
* @param executor the executor to run the asynchronous data fetcher in
4343
*
44-
* @return a {@link DataFetcher} that will run the wrappedDataFetcher asynchronously in
44+
* @return a {@link DataFetcher} that will run the wrappedDataFetcher asynchronously in
4545
* the given {@link Executor}
4646
*/
47-
public static <T> AsynchronousDataFetcher<T> asyncWithExecutor(DataFetcher<T> wrappedDataFetcher,
48-
Executor executor) {
49-
return new AsynchronousDataFetcher<>(wrappedDataFetcher, executor);
47+
public static <T> AsyncDataFetcher<T> async(DataFetcher<T> wrappedDataFetcher, Executor executor) {
48+
return new AsyncDataFetcher<>(wrappedDataFetcher, executor);
5049
}
51-
50+
5251
private final DataFetcher<T> wrappedDataFetcher;
5352
private final Executor executor;
5453

55-
public AsynchronousDataFetcher(DataFetcher<T> wrappedDataFetcher) {
54+
public AsyncDataFetcher(DataFetcher<T> wrappedDataFetcher) {
5655
this(wrappedDataFetcher, ForkJoinPool.commonPool());
5756
}
5857

59-
public AsynchronousDataFetcher(DataFetcher<T> wrappedDataFetcher, Executor executor) {
58+
public AsyncDataFetcher(DataFetcher<T> wrappedDataFetcher, Executor executor) {
6059
this.wrappedDataFetcher = assertNotNull(wrappedDataFetcher, "wrappedDataFetcher can't be null");
6160
this.executor = assertNotNull(executor, "executor can't be null");
6261
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package graphql.schema
2+
3+
import spock.lang.Specification
4+
5+
import java.util.concurrent.CompletableFuture
6+
import java.util.concurrent.Executor
7+
import java.util.concurrent.Executors
8+
9+
class AsyncDataFetcherTest extends Specification {
10+
11+
DataFetcher wrappedDataFetcher = { env -> "value" }
12+
13+
def "A data fetcher can be made asynchronous with AsynchronousDataFetcher#async"() {
14+
given:
15+
DataFetchingEnvironment environment = Mock(DataFetchingEnvironment)
16+
17+
when:
18+
DataFetcher asyncDataFetcher = AsyncDataFetcher.async(wrappedDataFetcher)
19+
20+
then:
21+
asyncDataFetcher.get(environment) instanceof CompletableFuture
22+
asyncDataFetcher.get(environment).get() == "value"
23+
}
24+
25+
def "will accept its own executor"() {
26+
given:
27+
DataFetchingEnvironment environment = Mock(DataFetchingEnvironment)
28+
29+
when:
30+
Executor executor = Executors.newSingleThreadExecutor()
31+
DataFetcher asyncDataFetcher = AsyncDataFetcher.async(wrappedDataFetcher, executor)
32+
33+
then:
34+
asyncDataFetcher.get(environment) instanceof CompletableFuture
35+
asyncDataFetcher.get(environment).get() == "value"
36+
}
37+
}

src/test/groovy/graphql/schema/AsynchronousDataFetcherTest.groovy

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)