Skip to content

Commit aa07818

Browse files
author
Karthick Sankarachary
committed
Add serial and parallel flavors of the async strategy
1 parent eb32a8e commit aa07818

File tree

4 files changed

+30
-16
lines changed

4 files changed

+30
-16
lines changed

build.gradle

-6
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,9 @@ artifacts {
5353

5454
allprojects {
5555
tasks.withType(Javadoc) {
56-
// exclude('**/antlr/**')
5756
}
5857

5958
tasks.withType(JavaCompile) {
60-
// doFirst {
61-
// if (sourceCompatibility == '1.6' && System.env.JDK6_HOME != null) {
62-
// options.bootClasspath = "$System.env.JDK6_HOME/jre/lib/rt.jar"
63-
// }
64-
// }
6559
}
6660

6761
}

src/main/java/graphql/async/GraphQL.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class GraphQL extends graphql.GraphQL {
3030
*/
3131
public GraphQL(GraphQLSchema graphQLSchema) {
3232
//noinspection deprecation
33-
this(graphQLSchema, new AsyncExecutionStrategy());
33+
this(graphQLSchema, AsyncExecutionStrategy.parallel());
3434
}
3535

3636

@@ -44,7 +44,7 @@ public GraphQL(GraphQLSchema graphQLSchema) {
4444
*/
4545
public GraphQL(GraphQLSchema graphQLSchema, ExecutionStrategy queryStrategy) {
4646
//noinspection deprecation
47-
super(graphQLSchema, queryStrategy);
47+
super(graphQLSchema, queryStrategy, AsyncExecutionStrategy.parallel());
4848
}
4949

5050
/**
@@ -60,6 +60,7 @@ public GraphQL(GraphQLSchema graphQLSchema, ExecutionStrategy queryStrategy,
6060
ExecutionStrategy mutationStrategy) {
6161
super(graphQLSchema, queryStrategy, mutationStrategy);
6262
assert queryStrategy instanceof AsyncExecutionStrategy;
63+
assert mutationStrategy instanceof AsyncExecutionStrategy;
6364
}
6465

6566
/**
@@ -75,8 +76,8 @@ public static Builder newAsyncGraphQL(GraphQLSchema graphQLSchema) {
7576
public static class Builder {
7677

7778
private GraphQLSchema graphQLSchema;
78-
private ExecutionStrategy queryExecutionStrategy = new SimpleExecutionStrategy();
79-
private ExecutionStrategy mutationExecutionStrategy = new SimpleExecutionStrategy();
79+
private ExecutionStrategy queryExecutionStrategy = AsyncExecutionStrategy.parallel();
80+
private ExecutionStrategy mutationExecutionStrategy = AsyncExecutionStrategy.serial();
8081

8182
public Builder(GraphQLSchema graphQLSchema) {
8283
this.graphQLSchema = graphQLSchema;

src/main/java/graphql/execution/AsyncExecutionStrategy.java

+24-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.concurrent.ForkJoinPool;
1212
import java.util.function.Function;
1313
import java.util.stream.Collectors;
14+
import java.util.stream.Stream;
1415

1516
import graphql.ExceptionWhileDataFetching;
1617
import graphql.ExecutionResult;
@@ -41,13 +42,31 @@
4142
*/
4243
public class AsyncExecutionStrategy extends ExecutionStrategy {
4344

44-
ExecutorService executorService;
45+
protected ExecutorService executorService;
46+
protected boolean serial;
4547

46-
public AsyncExecutionStrategy() {
47-
this(ForkJoinPool.commonPool());
48+
public static AsyncExecutionStrategy serial() {
49+
return new AsyncExecutionStrategy(true);
4850
}
4951

50-
public AsyncExecutionStrategy(ExecutorService executorService) {
52+
public static AsyncExecutionStrategy serial(ExecutorService executorService) {
53+
return new AsyncExecutionStrategy(true, executorService);
54+
}
55+
56+
public static AsyncExecutionStrategy parallel() {
57+
return new AsyncExecutionStrategy(false);
58+
}
59+
60+
public static AsyncExecutionStrategy parallel(ExecutorService executorService) {
61+
return new AsyncExecutionStrategy(false, executorService);
62+
}
63+
64+
private AsyncExecutionStrategy(boolean serial) {
65+
this(serial, ForkJoinPool.commonPool());
66+
}
67+
68+
protected AsyncExecutionStrategy(boolean serial, ExecutorService executorService) {
69+
this.serial = serial;
5170
this.executorService = executorService;
5271
}
5372

@@ -68,7 +87,7 @@ public ExecutionResult execute(final ExecutionContext executionContext,
6887
Set<String> fieldNames = fields.keySet();
6988

7089
// Create tasks to resolve each of the fields
71-
fieldNames.stream().forEach((fieldName) -> {
90+
(serial ? fieldNames.stream() : fieldNames.parallelStream()).forEach((fieldName) -> {
7291
fieldFutures.put(fieldName, CompletableFuture.supplyAsync(
7392
() -> resolveField(executionContext, parentType, source, fields.get(fieldName)),
7493
executorService));

src/test/groovy/graphql/execution/AsyncExecutionStrategyTest.groovy

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class AsyncExecutionStrategyTest extends Specification {
6464
new ThreadPoolExecutor.CallerRunsPolicy());
6565

6666
def graphQL = GraphQL.newAsyncGraphQL(NewsSchema.newsSchema)
67-
.queryExecutionStrategy(new AsyncExecutionStrategy(threadPoolExecutor))
67+
.queryExecutionStrategy(AsyncExecutionStrategy.serial(threadPoolExecutor))
6868
.build()
6969

7070
def received = new AtomicReference<Map>()

0 commit comments

Comments
 (0)