|
1 | 1 | # graphql-complexity |
2 | 2 | Python library to compute the complexity of a GraphQL operation |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +## Usage |
| 8 | +The library uses the query complexity algorithm to compute the complexity of a GraphQL operation. The algorithm is |
| 9 | +based on the number of fields requested in the operation and the depth of the query. |
| 10 | + |
| 11 | +```python |
| 12 | +from graphql_complexity import (get_complexity, SimpleEstimator) |
| 13 | + |
| 14 | +query = """ |
| 15 | + query SomeQuery { |
| 16 | + user { |
| 17 | + id |
| 18 | + name |
| 19 | + } |
| 20 | + } |
| 21 | +""" |
| 22 | + |
| 23 | +complexity = get_complexity(query, estimator=SimpleEstimator(complexity=1, multiplier=1)) |
| 24 | +if complexity > 10: |
| 25 | + raise Exception("Query is too complex") |
| 26 | +``` |
| 27 | + |
| 28 | +## Estimators |
| 29 | +In order to get the complexity of a query, an estimator needs to be defined. The main responsibility of |
| 30 | +the estimator is to give each node an integer value representing its complexity and (optionally) a |
| 31 | +multiplier that reflects the complexity in relation to the depth of the query. |
| 32 | + |
| 33 | +There are two built-in estimators, plus the capability to create any new estimator by |
| 34 | +implementing the `ComplexityEstimator` interface. |
| 35 | + |
| 36 | +### Estimate fields complexity based on constants for complexity and multiplier |
| 37 | + |
| 38 | +This estimator assigns a **constant** complexity value to each field and multiplies |
| 39 | +it by another **constant** which is propagated along the depth of the query. |
| 40 | + |
| 41 | +```python |
| 42 | +from graphql_complexity import SimpleEstimator |
| 43 | + |
| 44 | + |
| 45 | +estimator = SimpleEstimator(complexity=2, multiplier=1) |
| 46 | +``` |
| 47 | + |
| 48 | +Given the following query: |
| 49 | +```qgl |
| 50 | +query { |
| 51 | + user { |
| 52 | + name |
| 53 | + email |
| 54 | + } |
| 55 | +} |
| 56 | +``` |
| 57 | +As the complexity and multiplier are constant, the complexity of the fields will be: |
| 58 | + |
| 59 | +| Field | Complexity | |
| 60 | +|-------|---------------| |
| 61 | +| user | `1` | |
| 62 | +| name | `2 * (2 * 1)` | |
| 63 | +| email | `2 * (2 * 1)` | |
| 64 | + |
| 65 | +And the total complexity will be `6`. |
| 66 | + |
| 67 | +### Define fields complexity using schema directives |
| 68 | + |
| 69 | +Assigns a complexity value to each field and multiplies it by the depth of the query. |
| 70 | +It also supports the `@complexity` directive to assign a custom complexity value to a field. |
| 71 | + |
| 72 | +This approach requires to provide the schema to the estimator. |
| 73 | + |
| 74 | +```python |
| 75 | +from graphql_complexity import DirectivesEstimator |
| 76 | + |
| 77 | + |
| 78 | +schema = """ |
| 79 | +directive @complexity( |
| 80 | + value: Int! |
| 81 | +) on FIELD_DEFINITION |
| 82 | +
|
| 83 | +type Query { |
| 84 | + oneField: String @complexity(value: 5) |
| 85 | + otherField: String @complexity(value: 1) |
| 86 | + withoutDirective: String |
| 87 | +} |
| 88 | +""" |
| 89 | + |
| 90 | +estimator = DirectivesEstimator(schema) |
| 91 | +``` |
| 92 | + |
| 93 | +Given the schema from above and the following query: |
| 94 | +```qgl |
| 95 | +query { |
| 96 | + oneField |
| 97 | + otherField |
| 98 | + withoutDirective |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +The complexity of the fields will be: |
| 103 | + |
| 104 | +| Field | Complexity | Comment | |
| 105 | +|------------------|------------|---------------------------------------------------------------------------------------------------| |
| 106 | +| oneField | `5` | | |
| 107 | +| otherField | `1` | | |
| 108 | +| withoutDirective | `1` | The default complexity for fields without directive is `1`, this can be modified by parameters. | |
| 109 | + |
| 110 | +And the total complexity will be `7`. |
| 111 | + |
| 112 | +### Create a custom estimator |
| 113 | +This option allows to define a custom estimator to compute the complexity of a field using the `ComplexityEstimator` interface. For example: |
| 114 | + |
| 115 | +```python |
| 116 | +from graphql_complexity import ComplexityEstimator |
| 117 | + |
| 118 | + |
| 119 | +class CustomEstimator(ComplexityEstimator): |
| 120 | + def get_field_complexity(self, node, key, parent, path, ancestors) -> int: |
| 121 | + if node.name.value == "specificField": |
| 122 | + return 100 |
| 123 | + return 1 |
| 124 | + |
| 125 | + def get_field_multiplier(self, node, key, parent, path, ancestors) -> int: |
| 126 | + return 1 |
| 127 | +``` |
| 128 | + |
| 129 | + |
| 130 | +## Supported libraries (based on GraphQL-core) |
| 131 | +The library is compatible with the following GraphQL libraries: |
| 132 | + |
| 133 | +### Strawberry |
| 134 | + |
| 135 | + |
| 136 | +The library is compatible with [strawberry-graphql](https://pypi.org/project/strawberry-graphql/). |
| 137 | +To use the library with strawberry-graphql, you need to install the library with the `strawberry-graphql` extra. |
| 138 | +```shell |
| 139 | +poetry install --extras strawberry-graphql |
| 140 | +``` |
| 141 | + |
| 142 | +To use the library with [strawberry-graphql](https://pypi.org/project/strawberry-graphql/), you need to use the `build_complexity_extension` method to build |
| 143 | +the complexity extension and add it to the schema. This method receives an estimator and returns a complexity |
| 144 | +extension that can be added to the schema. |
| 145 | + |
| 146 | +```python |
| 147 | +import strawberry |
| 148 | +from graphql_complexity.extensions.strawberry_graphql import build_complexity_extension |
| 149 | + |
| 150 | +@strawberry.type |
| 151 | +class Query: |
| 152 | + @strawberry.field() |
| 153 | + def hello_world(self) -> str: |
| 154 | + return "Hello world!" |
| 155 | + |
| 156 | +extension = build_complexity_extension() |
| 157 | +schema = strawberry.Schema(query=Query, extensions=[extension]) |
| 158 | + |
| 159 | +schema.execute_sync("query { helloWorld }") |
| 160 | +``` |
| 161 | +The `build_complexity_extension` method accepts an estimator as optional argument giving the possibility to use one |
| 162 | +of the built-in estimators or a custom estimator. |
| 163 | + |
| 164 | +## Credits |
| 165 | + |
| 166 | +Estimators idea was heavily inspired by [graphql-query-complexity](https://github.com/slicknode/graphql-query-complexity). |
0 commit comments