@@ -2,6 +2,7 @@ package graphql
22
33import graphql.analysis.MaxQueryComplexityInstrumentation
44import graphql.analysis.MaxQueryDepthInstrumentation
5+ import graphql.execution.batched.BatchedExecutionStrategy
56import graphql.language.SourceLocation
67import graphql.schema.DataFetcher
78import graphql.schema.DataFetchingEnvironment
@@ -18,6 +19,7 @@ import graphql.validation.ValidationErrorType
1819import spock.lang.Specification
1920import spock.lang.Unroll
2021
22+ import java.util.concurrent.CompletableFuture
2123import java.util.function.UnaryOperator
2224
2325import 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