@@ -19,6 +19,10 @@ npm install -S graphql-query-complexity
1919Create the rule with a maximum query complexity:
2020
2121``` javascript
22+ import queryComplexity, {
23+ simpleEstimator
24+ } from ' graphql-query-complexity' ;
25+
2226const rule = queryComplexity ({
2327 // The maximum allowed query complexity, queries above this threshold will be rejected
2428 maximumComplexity: 1000 ,
@@ -35,10 +39,69 @@ const rule = queryComplexity({
3539 // Optional function to create a custom error
3640 createError : (max : number , actual : number ) => {
3741 return new GraphQLError (` Query is too complex: ${ actual} . Maximum allowed complexity: ${ max} ` );
38- }
42+ },
43+
44+ // Add any number of estimators. The estimators are invoked in order, the first
45+ // numeric value that is being returned by an estimator is used as the field complexity.
46+ // If no estimator returns a value, an exception is raised.
47+ estimators: [
48+ // Add more estimators here...
49+
50+ // This will assign each field a complexity of 1 if no other estimator
51+ // returned a value.
52+ simpleEstimator ({
53+ defaultComplexity: 1
54+ })
55+ ]
3956});
4057```
4158
59+ ## Configuration / Complexity Estimators
60+
61+ The complexity calculation of a GraphQL query can be customized with so called complexity estimators.
62+ A complexity estimator is a simple function that calculates the complexity for a field. You can add
63+ any number of complexity estimators to the rule, which are then executed one after another.
64+ The first estimator that returns a numeric complexity value determines the complexity for that field.
65+
66+ At least one estimator has to return a complexity value, otherwise an exception is raised. You can
67+ for example use the [ simpleEstimator] ( ./src/estimators/simple/README.md ) as the last estimator
68+ in your chain to define a default value.
69+
70+ You can use any of the available estimators to calculate the complexity of a field
71+ or write your own:
72+
73+ * ** [ ` simpleEstimator ` ] ( src/estimators/simple/README.md ) :** The simple estimator returns a fixed complexity for each field. Can be used as
74+ last estimator in the chain for a default value.
75+ * ** [ ` fieldConfigEstimator ` ] ( src/estimators/simple/README.md ) :** The field config estimator lets you set a numeric value or a custom estimator
76+ function in the field config of your schema.
77+ * ** [ ` legacyEstimator ` ] ( src/estimators/legacy/README.md ) :** The legacy estimator implements the logic of previous versions. Can be used
78+ to gradually migrate your codebase to new estimators.
79+ * PR welcome...
80+
81+
82+ ## Creating Custom Estimators
83+
84+ An estimator has the following function signature:
85+
86+ ``` typescript
87+ type ComplexityEstimatorArgs = {
88+ // The composite type (interface, object, union) that the evaluated field belongs to
89+ type: GraphQLCompositeType ,
90+
91+ // The GraphQLField that is being evaluated
92+ field: GraphQLField <any , any >,
93+
94+ // The input arguments of the field
95+ args: {[key : string ]: any },
96+
97+ // The complexity of all child selections for that field
98+ childComplexity: number
99+ }
100+
101+ type ComplexityEstimator = (options : ComplexityEstimatorArgs ) => number | void ;
102+ ```
103+
104+
42105## Customizing complexity calculation
43106
44107By default, every field gets a complexity of 1. Let's look at the following example query:
0 commit comments