Skip to content

Commit b9113d6

Browse files
committed
Ignore unknown fragments #35
1 parent 0249e80 commit b9113d6

File tree

2 files changed

+58
-17
lines changed

2 files changed

+58
-17
lines changed

src/QueryComplexity.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -278,24 +278,26 @@ export default class QueryComplexity {
278278
}
279279
case Kind.FRAGMENT_SPREAD: {
280280
const fragment = this.context.getFragment(childNode.name.value);
281-
const fragmentType = assertCompositeType(
282-
this.context.getSchema().getType(fragment.typeCondition.name.value)
283-
);
284-
const nodeComplexity = this.nodeComplexity(fragment, fragmentType);
285-
if (isAbstractType(fragmentType)) {
286-
// Add fragment complexity for all possible types
287-
complexities = addComplexities(
288-
nodeComplexity,
289-
complexities,
290-
this.context.getSchema().getPossibleTypes(fragmentType).map(t => t.name),
291-
);
292-
} else {
293-
// Add complexity for object type
294-
complexities = addComplexities(
295-
nodeComplexity,
296-
complexities,
297-
[fragmentType.name],
281+
if (fragment) {
282+
const fragmentType = assertCompositeType(
283+
this.context.getSchema().getType(fragment.typeCondition.name.value)
298284
);
285+
const nodeComplexity = this.nodeComplexity(fragment, fragmentType);
286+
if (isAbstractType(fragmentType)) {
287+
// Add fragment complexity for all possible types
288+
complexities = addComplexities(
289+
nodeComplexity,
290+
complexities,
291+
this.context.getSchema().getPossibleTypes(fragmentType).map(t => t.name),
292+
);
293+
} else {
294+
// Add complexity for object type
295+
complexities = addComplexities(
296+
nodeComplexity,
297+
complexities,
298+
[fragmentType.name],
299+
);
300+
}
299301
}
300302
break;
301303
}

src/__tests__/QueryComplexity-test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,45 @@ describe('QueryComplexity analysis', () => {
166166
expect(visitor.complexity).to.equal(0);
167167
});
168168

169+
it('should ignore unknown fragments', () => {
170+
const ast = parse(`
171+
query {
172+
...UnknownFragment
173+
variableScalar(count: 100)
174+
}
175+
`);
176+
177+
const context = new CompatibleValidationContext(schema, ast, typeInfo);
178+
const visitor = new ComplexityVisitor(context, {
179+
maximumComplexity: 100,
180+
estimators: [
181+
simpleEstimator({defaultComplexity: 10})
182+
]
183+
});
184+
185+
visit(ast, visitWithTypeInfo(typeInfo, visitor));
186+
expect(visitor.complexity).to.equal(10);
187+
});
188+
189+
it('should ignore unused variables', () => {
190+
const ast = parse(`
191+
query ($unusedVar: ID!) {
192+
variableScalar(count: 100)
193+
}
194+
`);
195+
196+
const context = new CompatibleValidationContext(schema, ast, typeInfo);
197+
const visitor = new ComplexityVisitor(context, {
198+
maximumComplexity: 100,
199+
estimators: [
200+
simpleEstimator({defaultComplexity: 10})
201+
]
202+
});
203+
204+
visit(ast, visitWithTypeInfo(typeInfo, visitor));
205+
expect(visitor.complexity).to.equal(10);
206+
});
207+
169208
it('should report error above threshold', () => {
170209
const ast = parse(`
171210
query {

0 commit comments

Comments
 (0)