Skip to content

Commit 5efc848

Browse files
committed
fix batched execution (#695)
fix handling of non batched data fetchers returning CompletableFuture (cherry picked from commit 5b5a30f)
1 parent 3c8571e commit 5efc848

2 files changed

Lines changed: 150 additions & 4 deletions

File tree

‎src/main/java/graphql/execution/batched/UnbatchedDataFetcher.java‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package graphql.execution.batched;
22

33

4+
import graphql.execution.Async;
45
import graphql.schema.DataFetcher;
56
import graphql.schema.DataFetchingEnvironment;
67

78
import java.util.ArrayList;
89
import java.util.List;
10+
import java.util.concurrent.CompletableFuture;
11+
import java.util.concurrent.CompletionStage;
912

1013
import static graphql.schema.DataFetchingEnvironmentBuilder.newDataFetchingEnvironment;
1114

@@ -24,15 +27,23 @@ public UnbatchedDataFetcher(DataFetcher delegate) {
2427

2528

2629
@Override
27-
public Object get(DataFetchingEnvironment environment) {
30+
public CompletableFuture<List<Object>> get(DataFetchingEnvironment environment) {
2831
List<Object> sources = environment.getSource();
29-
List<Object> results = new ArrayList<>();
32+
List<CompletableFuture<Object>> results = new ArrayList<>();
3033
for (Object source : sources) {
3134

3235
DataFetchingEnvironment singleEnv = newDataFetchingEnvironment(environment)
3336
.source(source).build();
34-
results.add(delegate.get(singleEnv));
37+
results.add(getResult(delegate.get(singleEnv)));
3538
}
36-
return results;
39+
40+
return Async.each(results);
41+
}
42+
43+
private CompletableFuture<Object> getResult(Object rawResult) {
44+
if (!(rawResult instanceof CompletionStage)) {
45+
return CompletableFuture.completedFuture(rawResult);
46+
}
47+
return ((CompletionStage) rawResult).toCompletableFuture();
3748
}
3849
}

‎src/test/groovy/graphql/GraphQLTest.groovy‎

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package graphql
22

33
import graphql.analysis.MaxQueryComplexityInstrumentation
44
import graphql.analysis.MaxQueryDepthInstrumentation
5+
import graphql.execution.batched.BatchedExecutionStrategy
56
import graphql.language.SourceLocation
67
import graphql.schema.DataFetcher
78
import graphql.schema.DataFetchingEnvironment
@@ -18,6 +19,7 @@ import graphql.validation.ValidationErrorType
1819
import spock.lang.Specification
1920
import spock.lang.Unroll
2021

22+
import java.util.concurrent.CompletableFuture
2123
import java.util.function.UnaryOperator
2224

2325
import static graphql.ExecutionInput.Builder
@@ -671,4 +673,137 @@ class GraphQLTest extends Specification {
671673
"{ f2:field {scalar} f1: field{scalar} f3: field {scalar}}" | _
672674
}
673675
676+
@Unroll
677+
def "validation error with (#instrumentationName)"() {
678+
given:
679+
GraphQLObjectType foo = newObject()
680+
.name("Foo")
681+
.withInterface(new GraphQLTypeReference("Node"))
682+
.field(
683+
{ field ->
684+
field
685+
.name("id")
686+
.type(Scalars.GraphQLID)
687+
} as UnaryOperator)
688+
.build()
689+
690+
GraphQLInterfaceType node = GraphQLInterfaceType.newInterface()
691+
.name("Node")
692+
.field(
693+
{ field ->
694+
field
695+
.name("id")
696+
.type(Scalars.GraphQLID)
697+
} as UnaryOperator)
698+
.typeResolver({ type -> foo })
699+
.build()
700+
701+
GraphQLObjectType query = newObject()
702+
.name("RootQuery")
703+
.field(
704+
{ field ->
705+
field
706+
.name("a")
707+
.type(node)
708+
} as UnaryOperator)
709+
.build()
710+
711+
GraphQLSchema schema = newSchema()
712+
.query(query)
713+
.build();
714+
715+
GraphQL graphQL = GraphQL.newGraphQL(schema)
716+
.instrumentation(instrumentation)
717+
.build()
718+
719+
ExecutionInput executionInput = newExecutionInput()
720+
.query("{a}")
721+
.build()
722+
723+
when:
724+
def result = graphQL.execute(executionInput);
725+
726+
then:
727+
result.errors.size() == 1
728+
result.errors[0].message.contains("Sub selection required")
729+
730+
where:
731+
instrumentationName | instrumentation
732+
'max query depth' | new MaxQueryDepthInstrumentation(10)
733+
'max query complexity' | new MaxQueryComplexityInstrumentation(10)
734+
}
735+
736+
737+
def "batched execution with non batched DataFetcher returning CompletableFuture"() {
738+
given:
739+
GraphQLObjectType foo = GraphQLObjectType.newObject()
740+
.name("Foo")
741+
.withInterface(new GraphQLTypeReference("Node"))
742+
.field(
743+
{ field ->
744+
field
745+
.name("id")
746+
.type(Scalars.GraphQLID)
747+
} as UnaryOperator)
748+
.build()
749+
750+
GraphQLInterfaceType node = GraphQLInterfaceType.newInterface()
751+
.name("Node")
752+
.field(
753+
{ field ->
754+
field
755+
.name("id")
756+
.type(Scalars.GraphQLID)
757+
} as UnaryOperator)
758+
.typeResolver(
759+
{
760+
env ->
761+
if (env.getObject() instanceof CompletableFuture) {
762+
throw new RuntimeException("This seems bad!");
763+
}
764+
765+
return foo
766+
})
767+
.build()
768+
769+
GraphQLObjectType query = GraphQLObjectType.newObject()
770+
.name("RootQuery")
771+
.field(
772+
{ field ->
773+
field
774+
.name("node")
775+
.dataFetcher(
776+
{ env ->
777+
CompletableFuture.supplyAsync({ ->
778+
Map<String, String> map = new HashMap<>()
779+
map.put("id", "abc")
780+
781+
return map
782+
})
783+
})
784+
.type(node)
785+
} as UnaryOperator)
786+
.build()
787+
788+
GraphQLSchema schema = GraphQLSchema.newSchema()
789+
.query(query)
790+
.build()
791+
792+
GraphQL graphQL = GraphQL.newGraphQL(schema)
793+
.queryExecutionStrategy(new BatchedExecutionStrategy())
794+
.mutationExecutionStrategy(new BatchedExecutionStrategy())
795+
.build();
796+
797+
ExecutionInput executionInput = ExecutionInput.newExecutionInput()
798+
.query("{node {id}}")
799+
.build()
800+
when:
801+
def result = graphQL
802+
.execute(executionInput)
803+
804+
then:
805+
result.getData() == [node: [id: "abc"]]
806+
}
807+
808+
674809
}

0 commit comments

Comments
 (0)