Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default non-defined graphql operations to have 0 complexity #89

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});