@@ -3,7 +3,6 @@ package graphql.normalized
33import graphql.ExecutionInput
44import graphql.GraphQL
55import graphql.TestUtil
6- import graphql.execution.AbortExecutionException
76import graphql.execution.CoercedVariables
87import graphql.execution.MergedField
98import graphql.execution.RawVariables
@@ -20,6 +19,9 @@ import graphql.util.TraverserContext
2019import graphql.util.TraverserVisitorStub
2120import spock.lang.Specification
2221
22+ import java.util.stream.Collectors
23+ import java.util.stream.IntStream
24+
2325import static graphql.TestUtil.schema
2426import static graphql.language.AstPrinter.printAst
2527import static graphql.parser.Parser.parseValue
@@ -2694,7 +2696,89 @@ fragment personName on Person {
26942696 ]
26952697 }
26962698
2697- def "max depth"() {
2699+ def "query cannot exceed max depth"() {
2700+ String schema = """
2701+ type Query {
2702+ animal : Animal
2703+ }
2704+ interface Animal {
2705+ name : String
2706+ friends : [Animal ]
2707+ }
2708+ type Bird implements Animal {
2709+ name : String
2710+ friends : [Animal ]
2711+ }
2712+ type Cat implements Animal {
2713+ name : String
2714+ friends : [Animal ]
2715+ breed : String
2716+ }
2717+ type Dog implements Animal {
2718+ name : String
2719+ breed : String
2720+ friends : [Animal ]
2721+ }
2722+ """
2723+ GraphQLSchema graphQLSchema = TestUtil.schema(schema)
2724+
2725+ // We generate two less fields than the given depth
2726+ // One is due to the top level field
2727+ // One is due to the leaf selection
2728+ def animalSubselection = IntStream.rangeClosed(1, queryDepth - 2)
2729+ .mapToObj {
2730+ ""
2731+ }
2732+ .reduce("CHILD") { acc, value ->
2733+ acc.replace("CHILD", "friends { CHILD }")
2734+ }
2735+ .replace("CHILD", "name")
2736+
2737+ // Note: there is a total of 51 fields here
2738+ String query = """
2739+ {
2740+ animal {
2741+ $animalSubselection
2742+ }
2743+ }
2744+ """
2745+
2746+ def limit = 50
2747+
2748+ assertValidQuery(graphQLSchema, query)
2749+
2750+ Document document = TestUtil.parseQuery(query)
2751+
2752+ when:
2753+ Exception exception
2754+ try {
2755+ ExecutableNormalizedOperationFactory.createExecutableNormalizedOperationWithRawVariables(
2756+ graphQLSchema,
2757+ document,
2758+ null,
2759+ RawVariables.emptyVariables(),
2760+ ExecutableNormalizedOperationFactory.Options.defaultOptions().maxChildrenDepth(limit))
2761+ } catch (Exception e) {
2762+ exception = e
2763+ }
2764+
2765+ then:
2766+ if (queryDepth > limit) {
2767+ assert exception != null
2768+ assert exception.message.contains("depth exceeded")
2769+ assert exception.message.contains("> 50")
2770+ } else {
2771+ assert exception == null
2772+ }
2773+
2774+ where:
2775+ _ | queryDepth
2776+ _ | 49
2777+ _ | 50
2778+ _ | 51
2779+ }
2780+
2781+ def "big query is fine as long as depth is under limit"() {
26982782 String schema = """
26992783 type Query {
27002784 animal : Animal
@@ -2725,6 +2809,13 @@ fragment personName on Person {
27252809 friends : [Friend ]
27262810 }
27272811 """
2812+
2813+ def garbageFields = IntStream.range(0, 1000)
2814+ .mapToObj {
2815+ """ test_$it : friends { name }"""
2816+ }
2817+ .collect(Collectors.joining("\n "))
2818+
27282819 GraphQLSchema graphQLSchema = TestUtil.schema(schema)
27292820
27302821 String query = """
@@ -2759,24 +2850,12 @@ fragment personName on Person {
27592850 breed
27602851 }
27612852 }
2762- pets {
2763- .. . on Dog {
2764- friends {
2765- pets {
2766- .. . on Cat {
2767- friends {
2768- name
2769- }
2770- }
2771- }
2772- }
2773- }
2774- }
27752853 }
27762854 }
27772855 .. . on Dog {
27782856 name
27792857 }
2858+ $garbageFields
27802859 }
27812860 }
27822861 """
@@ -2786,16 +2865,14 @@ fragment personName on Person {
27862865 Document document = TestUtil.parseQuery(query)
27872866
27882867 when:
2789- ExecutableNormalizedOperationFactory.createExecutableNormalizedOperationWithRawVariables(
2868+ def result = ExecutableNormalizedOperationFactory.createExecutableNormalizedOperationWithRawVariables(
27902869 graphQLSchema,
27912870 document,
27922871 null,
27932872 RawVariables.emptyVariables(),
27942873 ExecutableNormalizedOperationFactory.Options.defaultOptions().maxChildrenDepth(5))
27952874
27962875 then:
2797- def exception = thrown(AbortExecutionException)
2798- exception.message.contains("depth exceeded")
2799- exception.message.contains("> 5")
2876+ notThrown(Throwable)
28002877 }
28012878}
0 commit comments