Skip to content

Commit 6e59dd0

Browse files
authored
Merge pull request #7 from Checho3388/improve-readme
Improve readme
2 parents 52c7507 + 5599b5e commit 6e59dd0

File tree

2 files changed

+33
-68
lines changed

2 files changed

+33
-68
lines changed

.github/logo.png

17.5 KB
Loading

README.md

Lines changed: 33 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,38 @@
1-
# graphql-complexity
2-
Python library to compute the complexity of a GraphQL operation
1+
<img src="https://github.com/Checho3388/graphql-complexity/raw/main/.github/logo.png" width="150">
32

4-
![Build](https://github.com/Checho3388/graphql-complexity/actions/workflows/python-buildlu.yml/badge.svg)
3+
# GraphQL Complexity
4+
5+
Welcome to GraphQL-Complexity! This Python library provides functionality to compute the complexity of a GraphQL operation, contributing to better understanding and optimization of your GraphQL APIs. This library is designed to be stable, robust, and highly useful for developers working with GraphQL.
6+
7+
![Build](https://github.com/Checho3388/graphql-complexity/actions/workflows/python-build.yml/badge.svg)
58
[![PyPI](https://img.shields.io/pypi/v/graphql-complexity?label=pypi%20package)](https://pypi.org/project/graphql-complexity/)
69
[![codecov](https://codecov.io/gh/Checho3388/graphql-complexity/graph/badge.svg?token=4LH7AVN119)](https://codecov.io/gh/Checho3388/graphql-complexity)
710

11+
## Features
12+
- Compute complexity of GraphQL queries
13+
- Multiple built-in estimators for complexity computation
14+
- Customizable estimators for specific use cases
15+
- Support for Strawberry GraphQL library
16+
17+
818
## Installation (Quick Start)
9-
The library can be installed using pip:
19+
20+
You can install the library via pip:
21+
1022
```shell
1123
pip install graphql-complexity
1224
```
13-
To use `strawberry-graphql` integration, you need to install the library with the `strawberry-graphql` extra.
25+
26+
For Strawberry GraphQL integration, use the following command:
27+
1428
```shell
1529
pip install graphql-complexity[strawberry-graphql]
1630
```
1731

1832
## Getting Started
1933
Create a file named `complexity.py` with the following content:
2034
```python
21-
from graphql_complexity import (get_complexity, SimpleEstimator)
35+
from graphql_complexity import get_complexity, SimpleEstimator
2236
from graphql import build_schema
2337

2438

@@ -55,19 +69,12 @@ The algorithm visits each node of the query and computes the complexity of each
5569

5670

5771
## Estimators
58-
In order to get the complexity of a query, an estimator needs to be defined.
59-
60-
>The main responsibility of an estimator is to give each node an integer value representing its complexity and
61-
> (optionally) a multiplier that reflects the complexity in relation to the depth of the query.
6272

63-
There are two built-in estimators (`SimpleEstimator` and `DirectiveEstimator`), plus the capability to create any new
64-
estimator by implementing the `ComplexityEstimator` interface.
73+
GraphQL-Complexity provides various built-in estimators for computing query complexity:
6574

6675
### SimpleEstimator
67-
Estimate fields complexity based on constants for complexity and multiplier.
68-
69-
This estimator assigns a **constant** complexity value to each field and multiplies
70-
it by another **constant** which is propagated along the depth of the query.
76+
Estimate fields complexity based on constants for complexity and multiplier. This assigns a constant
77+
complexity value to each field and multiplies it by another constant, which is propagated along the depth of the query.
7178

7279
```python
7380
from graphql_complexity import SimpleEstimator
@@ -76,33 +83,10 @@ from graphql_complexity import SimpleEstimator
7683
estimator = SimpleEstimator(complexity=2)
7784
```
7885

79-
Given the following GraphQL query:
80-
```graphql
81-
query {
82-
user {
83-
name
84-
email
85-
}
86-
}
87-
```
88-
As the complexity and multiplier are constant, the complexity of the fields is:
89-
90-
| Field | Complexity |
91-
|-------|---------------|
92-
| user | `1` |
93-
| name | `2 * (2 * 1)` |
94-
| email | `2 * (2 * 1)` |
95-
96-
And the total complexity is `6`.
97-
9886
### DirectivesEstimator
9987

100-
Define fields complexity using schema directives.
101-
102-
Assigns a complexity value to each field and multiplies it by the depth of the query.
103-
It also supports the `@complexity` directive to assign a custom complexity value to a field.
104-
105-
This approach requires to provide the schema to the estimator.
88+
Define fields complexity using schema directives. This assigns a complexity value to each field and multiplies it
89+
by the depth of the query. It also supports the @complexity directive to assign a custom complexity value to a field.
10690

10791
```python
10892
from graphql_complexity import DirectivesEstimator
@@ -123,27 +107,8 @@ type Query {
123107
estimator = DirectivesEstimator(schema)
124108
```
125109

126-
Given the schema from above and the following query:
127-
```graphql
128-
query {
129-
oneField
130-
otherField
131-
withoutDirective
132-
}
133-
```
134-
135-
The complexity of the fields results in the following table:
136-
137-
| Field | Complexity | Comment |
138-
|------------------|------------|-------------------------------------------------------------------------------------------------|
139-
| oneField | `5` | Complexity given by `@complexity(value: 5)` |
140-
| otherField | `1` | Complexity given by `@complexity(value: 1)` |
141-
| withoutDirective | `1` | The default complexity for fields without directive is `1`, this can be modified by parameters. |
142-
143-
And the total complexity is `7`.
144-
145110
### Custom estimator
146-
This option allows to define a custom estimator to compute the complexity of a field using the `ComplexityEstimator` interface. For example:
111+
Custom estimators can be defined to compute the complexity of a field using the `ComplexityEstimator` interface.
147112

148113
```python
149114
from graphql_complexity import ComplexityEstimator
@@ -157,24 +122,24 @@ class CustomEstimator(ComplexityEstimator):
157122
```
158123

159124

160-
## Supported libraries (based on GraphQL-core)
125+
## Supported libraries
161126
This library is compatible with the following GraphQL libraries:
162127

163-
### Strawberry
128+
### Strawberry GraphQL
164129

165130
The library is compatible with [strawberry-graphql](https://pypi.org/project/strawberry-graphql/).
166-
To use the library with strawberry-graphql, you need to install the library with the `strawberry-graphql` extra.
131+
Use the following command to install the library with Strawberry support:
132+
167133
```shell
168134
poetry install --extras strawberry-graphql
169135
```
170136

171-
To use the library with [strawberry-graphql](https://pypi.org/project/strawberry-graphql/), you need to use the `build_complexity_extension` method to build
172-
the complexity extension and add it to the schema. This method receives an estimator and returns a complexity
173-
extension that can be added to the schema.
137+
To use the library with Strawberry GraphQL, use the `build_complexity_extension` method to build the complexity
138+
extension and add it to the schema. This method receives an estimator and returns a complexity extension that can be added to the schema.
174139

175140
```python
176141
import strawberry
177-
from graphql_complexity.estimators import SimpleEstimator
142+
from graphql_complexity import SimpleEstimator
178143
from graphql_complexity.extensions.strawberry_graphql import build_complexity_extension
179144

180145

0 commit comments

Comments
 (0)