MCPcopy Create free account
hub / github.com/parse-community/parse-server / visitSelectionSet

Function visitSelectionSet

src/GraphQL/helpers/queryComplexity.js:10–67  ·  view source on GitHub ↗
(selectionSet, depth, visitedFragments)

Source from the content-addressed store, hash-verified

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 }

Callers 1

calculateQueryComplexityFunction · 0.85

Calls 1

getMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…