(selectionSet, depth, visitedFragments)
| 8 | const { maxDepth: allowedMaxDepth, maxFields: allowedMaxFields } = limits; |
| 9 | |
| 10 | function visitSelectionSet(selectionSet, depth, visitedFragments) { |
| 11 | if (!selectionSet) { |
| 12 | return; |
| 13 | } |
| 14 | if ( |
| 15 | (allowedMaxFields !== undefined && allowedMaxFields !== -1 && totalFields > allowedMaxFields) || |
| 16 | (allowedMaxDepth !== undefined && allowedMaxDepth !== -1 && maxDepth > allowedMaxDepth) |
| 17 | ) { |
| 18 | return; |
| 19 | } |
| 20 | for (const selection of selectionSet.selections) { |
| 21 | if (selection.kind === 'Field') { |
| 22 | totalFields++; |
| 23 | const newDepth = depth + 1; |
| 24 | if (newDepth > maxDepth) { |
| 25 | maxDepth = newDepth; |
| 26 | } |
| 27 | if (selection.selectionSet) { |
| 28 | visitSelectionSet(selection.selectionSet, newDepth, visitedFragments); |
| 29 | } |
| 30 | } else if (selection.kind === 'InlineFragment') { |
| 31 | visitSelectionSet(selection.selectionSet, depth, visitedFragments); |
| 32 | } else if (selection.kind === 'FragmentSpread') { |
| 33 | const name = selection.name.value; |
| 34 | if (fragmentCache.has(name)) { |
| 35 | const cached = fragmentCache.get(name); |
| 36 | totalFields += cached.fields; |
| 37 | const adjustedDepth = depth + cached.maxDepthDelta; |
| 38 | if (adjustedDepth > maxDepth) { |
| 39 | maxDepth = adjustedDepth; |
| 40 | } |
| 41 | continue; |
| 42 | } |
| 43 | if (visitedFragments.has(name)) { |
| 44 | continue; |
| 45 | } |
| 46 | const fragment = fragments[name]; |
| 47 | if (fragment) { |
| 48 | if ( |
| 49 | (allowedMaxFields !== undefined && allowedMaxFields !== -1 && totalFields > allowedMaxFields) || |
| 50 | (allowedMaxDepth !== undefined && allowedMaxDepth !== -1 && maxDepth > allowedMaxDepth) |
| 51 | ) { |
| 52 | continue; |
| 53 | } |
| 54 | visitedFragments.add(name); |
| 55 | const savedFields = totalFields; |
| 56 | const savedMaxDepth = maxDepth; |
| 57 | maxDepth = depth; |
| 58 | visitSelectionSet(fragment.selectionSet, depth, visitedFragments); |
| 59 | const fieldsContribution = totalFields - savedFields; |
| 60 | const maxDepthDelta = maxDepth - depth; |
| 61 | fragmentCache.set(name, { fields: fieldsContribution, maxDepthDelta }); |
| 62 | maxDepth = Math.max(savedMaxDepth, maxDepth); |
| 63 | visitedFragments.delete(name); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | } |
no test coverage detected
searching dependent graphs…