Skip to content

Commit 1b08294

Browse files
committed
handled inline fragments with differing complexities
1 parent 55e568a commit 1b08294

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/analysis/ASTnodefunctions.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
DefinitionNode,
66
Kind,
77
SelectionNode,
8-
NamedTypeNode,
98
} from 'graphql';
109
import { FieldWeight, TypeWeightObject, Variables } from '../@types/buildTypeWeights';
1110
/**
@@ -160,13 +159,30 @@ class ASTParser {
160159

161160
selectionSetNode(node: SelectionSetNode, parentName: string): number {
162161
let complexity = 0;
162+
let maxFragmentComplexity = 0;
163163
// iterate shrough the 'selections' array on the seletion set node
164164
for (let i = 0; i < node.selections.length; i += 1) {
165165
// call the function to handle seletion nodes
166166
// pass the current parent through because selection sets act only as intermediaries
167-
complexity += this.selectionNode(node.selections[i], parentName);
167+
const selectionNode = node.selections[i];
168+
const selectionCost = this.selectionNode(node.selections[i], parentName);
169+
170+
// we need to get the largest possible complexity so we save the largest inline fragment
171+
// FIXME: Consider the case where 2 typed fragments are applicable
172+
// e.g. ...UnionType and ...PartofTheUnion
173+
// this case these complexities should be summed in order to be accurate
174+
// However an estimation suffice
175+
if (selectionNode.kind === Kind.INLINE_FRAGMENT) {
176+
if (!selectionNode.typeCondition) {
177+
// complexity is always applicable
178+
complexity += selectionCost;
179+
} else if (selectionCost > maxFragmentComplexity)
180+
maxFragmentComplexity = selectionCost;
181+
} else {
182+
complexity += selectionCost;
183+
}
168184
}
169-
return complexity;
185+
return complexity + maxFragmentComplexity;
170186
}
171187

172188
definitionNode(node: DefinitionNode): number {

0 commit comments

Comments
 (0)