Skip to content

Commit 36c53b1

Browse files
committed
add test
1 parent ae06ded commit 36c53b1

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/test/groovy/graphql/GraphQLTest.groovy

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package graphql
22

3+
import graphql.analysis.MaxQueryComplexityInstrumentation
34
import graphql.analysis.MaxQueryDepthInstrumentation
45
import graphql.language.SourceLocation
56
import graphql.schema.DataFetcher
@@ -627,4 +628,47 @@ class GraphQLTest extends Specification {
627628
"{ f2:field {field {field {scalar}}} f1: field{scalar} f3: field {scalar}}" | _
628629
}
629630
631+
@Unroll
632+
def "abort execution if complexity is too high (#query)"() {
633+
given:
634+
def foo = newObject()
635+
.name("Foo")
636+
.field(newFieldDefinition()
637+
.name("field")
638+
.type(new GraphQLTypeReference('Foo'))
639+
.build())
640+
.field(newFieldDefinition()
641+
.name("scalar")
642+
.type(GraphQLString)
643+
.build())
644+
.build()
645+
GraphQLSchema schema = newSchema().query(
646+
newObject()
647+
.name("RootQueryType")
648+
.field(newFieldDefinition()
649+
.name("field")
650+
.type(foo)
651+
.build()).build())
652+
.build()
653+
654+
MaxQueryComplexityInstrumentation maxQueryComplexityInstrumentation = new MaxQueryComplexityInstrumentation(3);
655+
656+
657+
def graphql = GraphQL.newGraphQL(schema).instrumentation(maxQueryComplexityInstrumentation).build()
658+
659+
when:
660+
def result = graphql.execute(query)
661+
662+
then:
663+
result.errors.size() == 1
664+
result.errors[0].message.contains("maximum query complexity exceeded")
665+
666+
where:
667+
query | _
668+
"{ field {field {field {field {scalar}}}} }" | _
669+
"{ field {field {field {scalar}}}} " | _
670+
"{ field {field {field {field {scalar}}}} }" | _
671+
"{ f2:field {scalar} f1: field{scalar} f3: field {scalar}}" | _
672+
}
673+
630674
}

0 commit comments

Comments
 (0)