Skip to content

Commit

Permalink
fix: default non-defined graphql operations to have 0 complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
BasKiers committed Sep 19, 2023
1 parent fa1f9d5 commit 4be85d8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/QueryComplexity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,13 @@ export default class QueryComplexity {
| FragmentDefinitionNode
| InlineFragmentNode
| OperationDefinitionNode,
typeDef: GraphQLObjectType | GraphQLInterfaceType | GraphQLUnionType
typeDef:
| GraphQLObjectType
| GraphQLInterfaceType
| GraphQLUnionType
| undefined
): number {
if (node.selectionSet) {
if (node.selectionSet && typeDef) {
let fields: GraphQLFieldMap<any, any> = {};
if (
typeDef instanceof GraphQLObjectType ||
Expand Down
21 changes: 21 additions & 0 deletions src/__tests__/QueryComplexity-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -891,4 +891,25 @@ describe('QueryComplexity analysis', () => {
expect(errors).to.have.length(1);
expect(errors[0].message).to.contain('INVALIDVALUE');
});

it('falls back to 0 complexity for GraphQL operations not supported by the schema', () => {
const ast = parse(`
subscription {
foo
}
`);

const errors = validate(schema, ast, [
createComplexityRule({
maximumComplexity: 1000,
estimators: [
simpleEstimator({
defaultComplexity: 1,
}),
],
}),
]);

expect(errors).to.have.length(0);
});
});

0 comments on commit 4be85d8

Please sign in to comment.