|
| 1 | +package graphql.validation |
| 2 | + |
| 3 | +import graphql.TestUtil |
| 4 | +import graphql.language.Document |
| 5 | +import graphql.parser.Parser |
| 6 | +import spock.lang.Specification |
| 7 | + |
| 8 | +class FragmentExpansionComplexityTest extends Specification { |
| 9 | + def schema = TestUtil.schema(''' |
| 10 | + type Query { node: Node } |
| 11 | + type Node { name: String child: Node } |
| 12 | + ''') |
| 13 | + |
| 14 | + def "F to G to J counts fields at every level but fragment spreads do not add depth"() { |
| 15 | + given: |
| 16 | + def query = ''' |
| 17 | + { node { ...F } } |
| 18 | + fragment F on Node { name ...G } |
| 19 | + fragment G on Node { child { ...J } } |
| 20 | + fragment J on Node { name } |
| 21 | + ''' |
| 22 | + |
| 23 | + expect: |
| 24 | + // Expanded selection: node { name child { name } }. |
| 25 | + assertComplexityBounds(query, 4, 4, 3) |
| 26 | + } |
| 27 | + |
| 28 | + def "repeating F G and J multiplies occurrences while their response fields merge"() { |
| 29 | + given: |
| 30 | + def query = ''' |
| 31 | + { node { ...F ...F } } |
| 32 | + fragment F on Node { name ...G ...G } |
| 33 | + fragment G on Node { child { ...J ...J } } |
| 34 | + fragment J on Node { name } |
| 35 | + ''' |
| 36 | + |
| 37 | + expect: |
| 38 | + // J contributes 1 occurrence; G contributes 1 + 2 = 3; F contributes 1 + 2 * 3 = 7. |
| 39 | + // The operation has 1 + 2 * 7 = 15 occurrences, but still merges to node { name child { name } }. |
| 40 | + assertComplexityBounds(query, 4, 15, 3) |
| 41 | + } |
| 42 | + |
| 43 | + def "diamond expansion counts shared J at every use in branch order #branches"() { |
| 44 | + given: |
| 45 | + def query = """ |
| 46 | + { node { ...F } } |
| 47 | + fragment F on Node { $branches } |
| 48 | + fragment G on Node { name ...J } |
| 49 | + fragment H on Node { child { ...J } ...J } |
| 50 | + fragment J on Node { name name } |
| 51 | + """ |
| 52 | + |
| 53 | + expect: |
| 54 | + // G contributes 3 occurrences and H contributes 5, including J at two response positions. |
| 55 | + // The operation has 1 + 3 + 5 = 9 occurrences and merges to node { name child { name } }. |
| 56 | + assertComplexityBounds(query, 4, 9, 3) |
| 57 | + |
| 58 | + where: |
| 59 | + branches << ['...G ...H', '...H ...G'] |
| 60 | + } |
| 61 | + |
| 62 | + def "shared F to G to J counts stay per operation with reversed operations #reverseOperations and definitions #reverseDefinitions"() { |
| 63 | + given: |
| 64 | + def first = 'query First { node { ...F } }' |
| 65 | + def second = 'query Second { node { child { ...F ...F } } }' |
| 66 | + def fragments = [ |
| 67 | + 'fragment F on Node { name ...G }', |
| 68 | + 'fragment G on Node { child { ...J } }', |
| 69 | + 'fragment J on Node { name }', |
| 70 | + ] |
| 71 | + def operations = reverseOperations ? second + first : first + second |
| 72 | + def definitions = reverseDefinitions ? fragments.reverse() : fragments |
| 73 | + def query = operations + definitions.join('\n') |
| 74 | + |
| 75 | + expect: |
| 76 | + // First has 4 merged fields, 4 occurrences and depth 3. |
| 77 | + // Second has 5 merged fields, 2 + 2 * 3 = 8 occurrences and depth 4. |
| 78 | + // Limits apply to each operation, not the sum; cached relative depth must fit either position. |
| 79 | + assertComplexityBounds(query, 5, 8, 4) |
| 80 | + |
| 81 | + where: |
| 82 | + reverseOperations | reverseDefinitions |
| 83 | + false | false |
| 84 | + false | true |
| 85 | + true | false |
| 86 | + true | true |
| 87 | + } |
| 88 | + |
| 89 | + private void assertComplexityBounds(String query, int mergedFields, int occurrences, int depth) { |
| 90 | + def document = Parser.parse(query) |
| 91 | + assert validate(document, mergedFields, occurrences, depth).empty |
| 92 | + assert validate(document, mergedFields - 1, occurrences, depth)*.validationErrorType == |
| 93 | + [ValidationErrorType.MaxQueryMergedFieldCountExceeded] |
| 94 | + assert validate(document, mergedFields, occurrences - 1, depth)*.validationErrorType == |
| 95 | + [ValidationErrorType.MaxQueryFieldOccurrencesExceeded] |
| 96 | + assert validate(document, mergedFields, occurrences, depth - 1)*.validationErrorType == |
| 97 | + [ValidationErrorType.MaxQueryDepthExceeded] |
| 98 | + } |
| 99 | + |
| 100 | + private List<ValidationError> validate(Document document, int mergedFields, int occurrences, int depth) { |
| 101 | + def limits = QueryComplexityLimits.newLimitsWithDefaults() |
| 102 | + .maxMergedFieldCount(mergedFields) |
| 103 | + .maxFieldOccurrences(occurrences) |
| 104 | + .maxDepth(depth) |
| 105 | + .build() |
| 106 | + new Validator().validateDocument(schema, document, { rule -> true }, Locale.ENGLISH, limits) |
| 107 | + } |
| 108 | +} |
0 commit comments