Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/ComplexityVisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,28 @@ export default class ComplexityVisitor {
this.SelectionSet = this.flattenFragmentSpreads;
}

flattenFragmentSpreads(selectionSet) {
const nextSelections = selectionSet.selections.flatMap((node) => {
if (node.kind === 'FragmentSpread') {
const fragment = this.context.getFragment(node.name.value);
flattenFragmentSpreads(selectionSet, visitedFragments) {

// Ensure that visitedFragments is a Set or initialize a new Set if not provided
visitedFragments = visitedFragments instanceof Set ? visitedFragments : new Set();

var nextSelections = selectionSet.selections.flatMap((node) => {
if (node.kind === 'FragmentSpread') {
var fragmentName = node.name.value;

if (visitedFragments.has(fragmentName)) {
return [];
}

var fragment = this.context.getFragment(fragmentName);

if (!fragment) return [];
return this.flattenFragmentSpreads(fragment.selectionSet).selections;
var fragment = this.context.getFragment(node.name.value);

// Add the fragment to the set before the recursive call
visitedFragments.add(fragmentName);

return this.flattenFragmentSpreads(fragment.selectionSet, visitedFragments).selections;
}

return node;
Expand Down