graphql-query-complexity
Advanced tools
@@ -8,2 +8,9 @@ "use strict"; | ||
}; | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; | ||
result["default"] = mod; | ||
return result; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -13,6 +20,40 @@ const graphql_1 = require("graphql"); | ||
const schema_1 = __importDefault(require("./fixtures/schema")); | ||
const QueryComplexity_1 = __importDefault(require("../QueryComplexity")); | ||
const QueryComplexity_1 = __importStar(require("../QueryComplexity")); | ||
const index_1 = require("../index"); | ||
describe('QueryComplexity analysis', () => { | ||
const typeInfo = new graphql_1.TypeInfo(schema_1.default); | ||
it('should calculate complexity', () => { | ||
const ast = graphql_1.parse(` | ||
query { | ||
variableScalar(count: 10) | ||
} | ||
`); | ||
const complexity = QueryComplexity_1.getComplexity({ | ||
estimators: [ | ||
index_1.simpleEstimator({ defaultComplexity: 1 }) | ||
], | ||
schema: schema_1.default, | ||
query: ast | ||
}); | ||
chai_1.expect(complexity).to.equal(1); | ||
}); | ||
it('should calculate complexity with variables', () => { | ||
const ast = graphql_1.parse(` | ||
query Q($count: Int) { | ||
variableScalar(count: $count) | ||
} | ||
`); | ||
const complexity = QueryComplexity_1.getComplexity({ | ||
estimators: [ | ||
index_1.fieldConfigEstimator(), | ||
index_1.simpleEstimator({ defaultComplexity: 1 }) | ||
], | ||
schema: schema_1.default, | ||
query: ast, | ||
variables: { | ||
count: 5, | ||
}, | ||
}); | ||
chai_1.expect(complexity).to.equal(50); | ||
}); | ||
it('should not allow negative cost', () => { | ||
@@ -19,0 +60,0 @@ const ast = graphql_1.parse(` |
/** | ||
* Created by Ivo Meißner on 28.07.17. | ||
*/ | ||
import { ValidationContext, FragmentDefinitionNode, OperationDefinitionNode, FieldNode, InlineFragmentNode, GraphQLField, GraphQLCompositeType } from 'graphql'; | ||
import { ValidationContext, FragmentDefinitionNode, OperationDefinitionNode, FieldNode, InlineFragmentNode, GraphQLField, GraphQLCompositeType, GraphQLSchema, DocumentNode } from 'graphql'; | ||
import { GraphQLUnionType, GraphQLObjectType, GraphQLInterfaceType, GraphQLError } from 'graphql'; | ||
@@ -30,2 +30,8 @@ declare module 'graphql/type/definition' { | ||
} | ||
export declare function getComplexity(options: { | ||
estimators: ComplexityEstimator[]; | ||
schema: GraphQLSchema; | ||
query: DocumentNode; | ||
variables?: Object; | ||
}): number; | ||
export default class QueryComplexity { | ||
@@ -32,0 +38,0 @@ context: ValidationContext; |
@@ -14,2 +14,15 @@ "use strict"; | ||
} | ||
function getComplexity(options) { | ||
const typeInfo = new graphql_1.TypeInfo(options.schema); | ||
const context = new graphql_1.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 | ||
}); | ||
graphql_1.visit(options.query, graphql_1.visitWithTypeInfo(typeInfo, visitor)); | ||
return visitor.complexity; | ||
} | ||
exports.getComplexity = getComplexity; | ||
class QueryComplexity { | ||
@@ -16,0 +29,0 @@ constructor(context, options) { |
{ | ||
"name": "graphql-query-complexity", | ||
"version": "0.2.3", | ||
"version": "0.3.0", | ||
"description": "Validation rule for GraphQL query complexity analysis", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -107,2 +107,3 @@ # GraphQL Query Complexity Analysis for graphql-js | ||
## Usage with express-graphql | ||
@@ -130,2 +131,39 @@ | ||
## Calculate query complexity | ||
If you want to calculate the complexity of a GraphQL query outside of the validation phase, for example to | ||
return the complexity value in a resolver, you can calculate the complexity via `getComplexity`: | ||
```javascript | ||
import { getComplexity, simpleEstimator } from 'graphql-query-complexity'; | ||
import { parse } from 'graphql'; | ||
// Import your schema or get it form the info object in your resolver | ||
import schema from './schema'; | ||
// You can also use gql template tag to get the parsed query | ||
const query = parse(` | ||
query Q($count: Int) { | ||
some_value | ||
some_list(count: $count) { | ||
some_child_value | ||
} | ||
} | ||
`); | ||
const complexity = getComplexity({ | ||
estimators: [ | ||
simpleEstimator({defaultComplexity: 1}) | ||
], | ||
schema, | ||
query, | ||
variables: { | ||
count: 10, | ||
}, | ||
}); | ||
console.log(complexity); // Output: 3 | ||
``` | ||
## Prior Art | ||
@@ -132,0 +170,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
133480
3.25%2073
2.98%173
28.15%