@@ -9,10 +9,7 @@ The library uses the query complexity algorithm to compute the complexity of a G
99based 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
1714query = """
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 ))
3124if 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
4639it 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
5548Given 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
7972This 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
8578schema = """
@@ -120,7 +113,7 @@ And the total complexity will be `7`.
120113This 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
126119class CustomEstimator (ComplexityEstimator ):
@@ -138,6 +131,8 @@ class CustomEstimator(ComplexityEstimator):
138131The library is compatible with the following GraphQL libraries:
139132
140133### Strawberry
134+
135+
141136The library is compatible with [ strawberry-graphql] ( https://pypi.org/project/strawberry-graphql/ ) .
142137To 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
152147import 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
156151class Query :
0 commit comments