Skip to content

Commit 7ee4cdd

Browse files
committed
docs: Update README.md
1 parent a069c52 commit 7ee4cdd

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

README.md

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ The library uses the query complexity algorithm to compute the complexity of a G
99
based on the number of fields requested in the operation and the depth of the query.
1010

1111
```python
12-
from graphql import parse, visit
13-
from graphql_complexity.visitor import ComplexityVisitor
14-
from graphql_complexity.estimators import SimpleEstimator
15-
12+
from graphql_complexity import (get_complexity, SimpleEstimator)
1613

1714
query = """
1815
query SomeQuery {
@@ -23,11 +20,7 @@ query = """
2320
}
2421
"""
2522

26-
ast = parse(query)
27-
visitor = ComplexityVisitor(estimator=SimpleEstimator(complexity=1, multiplier=1))
28-
visit(ast, visitor)
29-
30-
complexity = visitor.evaluate()
23+
complexity = get_complexity(query, estimator=SimpleEstimator(complexity=1, multiplier=1))
3124
if complexity > 10:
3225
raise Exception("Query is too complex")
3326
```
@@ -46,10 +39,10 @@ This estimator assigns a **constant** complexity value to each field and multipl
4639
it by another **constant** which is propagated along the depth of the query.
4740

4841
```python
49-
from graphql_complexity.estimators import SimpleEstimator
42+
from graphql_complexity import SimpleEstimator
5043

5144

52-
estimator = SimpleEstimator(complexity=1, multiplier=2)
45+
estimator = SimpleEstimator(complexity=2, multiplier=1)
5346
```
5447

5548
Given the following query:
@@ -69,7 +62,7 @@ As the complexity and multiplier are constant, the complexity of the fields will
6962
| name | `2 * (2 * 1)` |
7063
| email | `2 * (2 * 1)` |
7164

72-
And the total complexity will be `5`.
65+
And the total complexity will be `6`.
7366

7467
### Define fields complexity using schema directives
7568

@@ -79,7 +72,7 @@ It also supports the `@complexity` directive to assign a custom complexity value
7972
This approach requires to provide the schema to the estimator.
8073

8174
```python
82-
from graphql_complexity.estimators import DirectivesEstimator
75+
from graphql_complexity import DirectivesEstimator
8376

8477

8578
schema = """
@@ -120,7 +113,7 @@ And the total complexity will be `7`.
120113
This option allows to define a custom estimator to compute the complexity of a field using the `ComplexityEstimator` interface. For example:
121114

122115
```python
123-
from graphql_complexity.estimators import ComplexityEstimator
116+
from graphql_complexity import ComplexityEstimator
124117

125118

126119
class CustomEstimator(ComplexityEstimator):
@@ -138,6 +131,8 @@ class CustomEstimator(ComplexityEstimator):
138131
The library is compatible with the following GraphQL libraries:
139132

140133
### Strawberry
134+
135+
141136
The library is compatible with [strawberry-graphql](https://pypi.org/project/strawberry-graphql/).
142137
To use the library with strawberry-graphql, you need to install the library with the `strawberry-graphql` extra.
143138
```shell
@@ -150,7 +145,7 @@ extension that can be added to the schema.
150145

151146
```python
152147
import strawberry
153-
from graphql_complexity.extensions import build_complexity_extension
148+
from graphql_complexity.extensions.strawberry_graphql import build_complexity_extension
154149

155150
@strawberry.type
156151
class Query:

0 commit comments

Comments
 (0)