Skip to content

Commit 4be85d8

Browse files
committed
fix: default non-defined graphql operations to have 0 complexity
1 parent fa1f9d5 commit 4be85d8

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/QueryComplexity.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,13 @@ export default class QueryComplexity {
238238
| FragmentDefinitionNode
239239
| InlineFragmentNode
240240
| OperationDefinitionNode,
241-
typeDef: GraphQLObjectType | GraphQLInterfaceType | GraphQLUnionType
241+
typeDef:
242+
| GraphQLObjectType
243+
| GraphQLInterfaceType
244+
| GraphQLUnionType
245+
| undefined
242246
): number {
243-
if (node.selectionSet) {
247+
if (node.selectionSet && typeDef) {
244248
let fields: GraphQLFieldMap<any, any> = {};
245249
if (
246250
typeDef instanceof GraphQLObjectType ||

src/__tests__/QueryComplexity-test.ts

+21
Original file line numberDiff line numberDiff line change
@@ -891,4 +891,25 @@ describe('QueryComplexity analysis', () => {
891891
expect(errors).to.have.length(1);
892892
expect(errors[0].message).to.contain('INVALIDVALUE');
893893
});
894+
895+
it('falls back to 0 complexity for GraphQL operations not supported by the schema', () => {
896+
const ast = parse(`
897+
subscription {
898+
foo
899+
}
900+
`);
901+
902+
const errors = validate(schema, ast, [
903+
createComplexityRule({
904+
maximumComplexity: 1000,
905+
estimators: [
906+
simpleEstimator({
907+
defaultComplexity: 1,
908+
}),
909+
],
910+
}),
911+
]);
912+
913+
expect(errors).to.have.length(0);
914+
});
894915
});

0 commit comments

Comments
 (0)