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

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,33 @@ type ComplexityEstimatorArgs = {
type ComplexityEstimator = (options: ComplexityEstimatorArgs) => number | void;
```

## Calculate query complexity
```javascript
import { calculateComplexity, simpleEstimator } from "graphql-query-complexity/dist/QueryComplexity";
import { parse } from 'graphql';

// In a resolver the schema can be retrieved from the info argument.
const schema = undefined;
const query = parse(`
query {
some_value
some_list(count: 10) {
some_child_value
}
}
`);

const complexity = calculateComplexity({
estimators: [
simpleEstimator({defaultComplexity: 1})
],
schema,
query
});

console.log(complexity); // Output: 3
```

## Usage with express-graphql

To use the query complexity analysis validation rule with express-graphql, use something like the
Expand Down
22 changes: 22 additions & 0 deletions src/QueryComplexity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
InlineFragmentNode,
assertCompositeType,
GraphQLField, isCompositeType, GraphQLCompositeType, GraphQLFieldMap,
GraphQLSchema, DocumentNode, TypeInfo,
visit, visitWithTypeInfo
} from 'graphql';
import {
GraphQLUnionType,
Expand Down Expand Up @@ -74,6 +76,26 @@ function queryComplexityMessage(max: number, actual: number): string {
);
}

export function calculateComplexity(options: {
estimators: ComplexityEstimator[],
schema: GraphQLSchema,
query: DocumentNode,
variables?: Object
}): number {
const typeInfo = new TypeInfo(options.schema);

const context = new ValidationContext(options.schema, options.query, typeInfo);
const visitor = new QueryComplexity(context, {
// Maximum complexity does not matter since we're only interested in the calculated complexity.
maximumComplexity: Infinity,
estimators: options.estimators,
variables: options.variables
});

visit(options.query, visitWithTypeInfo(typeInfo, visitor));
return visitor.complexity;
}

export default class QueryComplexity {
context: ValidationContext;
complexity: number;
Expand Down
19 changes: 18 additions & 1 deletion src/__tests__/QueryComplexity-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {expect} from 'chai';

import schema from './fixtures/schema';

import ComplexityVisitor from '../QueryComplexity';
import ComplexityVisitor, {calculateComplexity} from '../QueryComplexity';
import {
simpleEstimator,
fieldConfigEstimator,
Expand All @@ -23,6 +23,23 @@ import {
describe('QueryComplexity analysis', () => {
const typeInfo = new TypeInfo(schema);

it('should calculate complexity', () => {
const ast = parse(`
query {
variableScalar(count: 10)
}
`);

const complexity = calculateComplexity({
estimators: [
simpleEstimator({defaultComplexity: 1})
],
schema,
query: ast
});
expect(complexity).to.equal(1);
});

it('should not allow negative cost', () => {
const ast = parse(`
query {
Expand Down