Skip to content

Commit 9cadc5a

Browse files
authored
fix: default non-defined graphql operations to have 0 complexity (#89)
1 parent c9e062a commit 9cadc5a

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
@@ -241,9 +241,13 @@ export default class QueryComplexity {
241241
| FragmentDefinitionNode
242242
| InlineFragmentNode
243243
| OperationDefinitionNode,
244-
typeDef: GraphQLObjectType | GraphQLInterfaceType | GraphQLUnionType
244+
typeDef:
245+
| GraphQLObjectType
246+
| GraphQLInterfaceType
247+
| GraphQLUnionType
248+
| undefined
245249
): number {
246-
if (node.selectionSet) {
250+
if (node.selectionSet && typeDef) {
247251
let fields: GraphQLFieldMap<any, any> = {};
248252
if (
249253
typeDef instanceof GraphQLObjectType ||

src/__tests__/QueryComplexity-test.ts

+21
Original file line numberDiff line numberDiff line change
@@ -918,4 +918,25 @@ describe('QueryComplexity analysis', () => {
918918
expect(errors).to.have.length(1);
919919
expect(errors[0].message).to.contain('INVALIDVALUE');
920920
});
921+
922+
it('falls back to 0 complexity for GraphQL operations not supported by the schema', () => {
923+
const ast = parse(`
924+
subscription {
925+
foo
926+
}
927+
`);
928+
929+
const errors = validate(schema, ast, [
930+
createComplexityRule({
931+
maximumComplexity: 1000,
932+
estimators: [
933+
simpleEstimator({
934+
defaultComplexity: 1,
935+
}),
936+
],
937+
}),
938+
]);
939+
940+
expect(errors).to.have.length(0);
941+
});
921942
});

0 commit comments

Comments
 (0)