You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
returnnewGraphQLError(`Query is too complex: ${actual}. Maximum allowed complexity: ${max}`);
38
-
}
50
+
},
51
+
52
+
// Add any number of estimators. The estimators are invoked in order, the first
53
+
// numeric value that is being returned by an estimator is used as the field complexity.
54
+
// If no estimator returns a value, an exception is raised.
55
+
estimators: [
56
+
// Add more estimators here...
57
+
58
+
// This will assign each field a complexity of 1 if no other estimator
59
+
// returned a value.
60
+
simpleEstimator({
61
+
defaultComplexity:1
62
+
})
63
+
]
39
64
});
40
65
```
41
66
42
-
## Customizing complexity calculation
67
+
## Configuration / Complexity Estimators
43
68
44
-
By default, every field gets a complexity of 1. Let's look at the following example query:
69
+
The complexity calculation of a GraphQL query can be customized with so called complexity estimators.
70
+
A complexity estimator is a simple function that calculates the complexity for a field. You can add
71
+
any number of complexity estimators to the rule, which are then executed one after another.
72
+
The first estimator that returns a numeric complexity value determines the complexity for that field.
45
73
46
-
```graphql
47
-
query {
48
-
posts(count: 10) {
49
-
title
50
-
text
51
-
}
52
-
}
53
-
```
74
+
At least one estimator has to return a complexity value, otherwise an exception is raised. You can
75
+
for example use the [simpleEstimator](./src/estimators/simple/README.md) as the last estimator
76
+
in your chain to define a default value.
54
77
55
-
This would result in a complexity of 3. The fields `posts`, `title` and `text` each add a complexity of 1.
56
-
If we assume that the posts field returns a list of 10 posts, the complexity estimation is pretty inaccurate.
78
+
You can use any of the available estimators to calculate the complexity of a field
79
+
or write your own:
57
80
58
-
When defining your fields, you have a two options to customize the calculation.
81
+
***[`simpleEstimator`](src/estimators/simple/README.md):** The simple estimator returns a fixed complexity for each field. Can be used as
82
+
last estimator in the chain for a default value.
83
+
***[`directiveEstimator`](src/estimators/directive/README.md):** Set the complexity via a directive in your
84
+
schema definition (for example via GraphQL SDL)
85
+
***[`fieldExtensionsEstimator`](src/estimators/fieldExtensions/README.md):** The field extensions estimator lets you set a numeric value or a custom estimator
86
+
function in the field config extensions of your schema.
87
+
* PRs welcome...
59
88
60
-
You can set a custom complexity in the field config:
89
+
Consult the documentation of each estimator for information about how to use them.
61
90
62
-
```javascript
63
-
constPost=newGraphQLObjectType({
64
-
name:'Post',
65
-
fields: () => ({
66
-
title: { type: GraphQLString },
67
-
text: { type: GraphQLString, complexity:5 },
68
-
}),
69
-
});
70
-
```
71
-
The same query would now result in a complexity of 7.
72
-
5 for the `text` field and 1 for each of the other fields.
91
+
## Creating Custom Estimators
73
92
74
-
You can also pass a calculation function in the field config to determine a custom complexity.
75
-
This function will provide the complexity of the child nodes as well as the field input arguments.
93
+
An estimator has the following function signature:
76
94
77
-
That way you can make a more realistic estimation of individual field complexity values:
95
+
```typescript
96
+
typeComplexityEstimatorArgs= {
97
+
// The composite type (interface, object, union) that the evaluated field belongs to
98
+
type:GraphQLCompositeType,
99
+
100
+
// The GraphQLField that is being evaluated
101
+
field:GraphQLField<any, any>,
102
+
103
+
// The input arguments of the field
104
+
args: {[key:string]:any},
105
+
106
+
// The complexity of all child selections for that field
0 commit comments