Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/main/java/graphql/execution/batched/UnbatchedDataFetcher.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package graphql.execution.batched;


import graphql.execution.Async;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;

import static graphql.schema.DataFetchingEnvironmentBuilder.newDataFetchingEnvironment;

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


@Override
public Object get(DataFetchingEnvironment environment) {
public CompletableFuture<List<Object>> get(DataFetchingEnvironment environment) {
List<Object> sources = environment.getSource();
List<Object> results = new ArrayList<>();
List<CompletableFuture<Object>> results = new ArrayList<>();
for (Object source : sources) {

DataFetchingEnvironment singleEnv = newDataFetchingEnvironment(environment)
.source(source).build();
results.add(delegate.get(singleEnv));
results.add(getResult(delegate.get(singleEnv)));
}
return results;

return Async.each(results);
}

private CompletableFuture<Object> getResult(Object rawResult) {
if (!(rawResult instanceof CompletionStage)) {
return CompletableFuture.completedFuture(rawResult);
}
return ((CompletionStage) rawResult).toCompletableFuture();
}
}
74 changes: 74 additions & 0 deletions src/test/groovy/graphql/GraphQLTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package graphql

import graphql.analysis.MaxQueryComplexityInstrumentation
import graphql.analysis.MaxQueryDepthInstrumentation
import graphql.execution.batched.BatchedExecutionStrategy
import graphql.language.SourceLocation
import graphql.schema.DataFetcher
import graphql.schema.DataFetchingEnvironment
Expand All @@ -19,6 +20,7 @@ import graphql.validation.ValidationErrorType
import spock.lang.Specification
import spock.lang.Unroll

import java.util.concurrent.CompletableFuture
import java.util.function.UnaryOperator

import static graphql.ExecutionInput.Builder
Expand Down Expand Up @@ -733,4 +735,76 @@ class GraphQLTest extends Specification {
}


def "batched execution with non batched DataFetcher returning CompletableFuture"() {
given:
GraphQLObjectType foo = GraphQLObjectType.newObject()
.name("Foo")
.withInterface(new GraphQLTypeReference("Node"))
.field(
{ field ->
field
.name("id")
.type(Scalars.GraphQLID)
} as UnaryOperator)
.build()

GraphQLInterfaceType node = GraphQLInterfaceType.newInterface()
.name("Node")
.field(
{ field ->
field
.name("id")
.type(Scalars.GraphQLID)
} as UnaryOperator)
.typeResolver(
{
env ->
if (env.getObject() instanceof CompletableFuture) {
throw new RuntimeException("This seems bad!");
}

return foo
})
.build()

GraphQLObjectType query = GraphQLObjectType.newObject()
.name("RootQuery")
.field(
{ field ->
field
.name("node")
.dataFetcher(
{ env ->
CompletableFuture.supplyAsync({ ->
Map<String, String> map = new HashMap<>()
map.put("id", "abc")

return map
})
})
.type(node)
} as UnaryOperator)
.build()

GraphQLSchema schema = GraphQLSchema.newSchema()
.query(query)
.build()

GraphQL graphQL = GraphQL.newGraphQL(schema)
.queryExecutionStrategy(new BatchedExecutionStrategy())
.mutationExecutionStrategy(new BatchedExecutionStrategy())
.build();

ExecutionInput executionInput = ExecutionInput.newExecutionInput()
.query("{node {id}}")
.build()
when:
def result = graphQL
.execute(executionInput)

then:
result.getData() == [node: [id: "abc"]]
}


}