Skip to content

Commit 4b968c4

Browse files
authored
📝 Update GraphQL docs, recommend Strawberry (fastapi#3981)
1 parent 1db0fc2 commit 4b968c4

8 files changed

Lines changed: 56 additions & 39 deletions

File tree

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,9 +418,9 @@ For a more complete example including more features, see the <a href="https://fa
418418
* A very powerful and easy to use **<abbr title="also known as components, resources, providers, services, injectables">Dependency Injection</abbr>** system.
419419
* Security and authentication, including support for **OAuth2** with **JWT tokens** and **HTTP Basic** auth.
420420
* More advanced (but equally easy) techniques for declaring **deeply nested JSON models** (thanks to Pydantic).
421+
* **GraphQL** integration with <a href="https://strawberry.rocks" class="external-link" target="_blank">Strawberry</a> and other libraries.
421422
* Many extra features (thanks to Starlette) as:
422423
* **WebSockets**
423-
* **GraphQL**
424424
* extremely easy tests based on `requests` and `pytest`
425425
* **CORS**
426426
* **Cookie Sessions**
@@ -447,7 +447,6 @@ Used by Starlette:
447447
* <a href="https://andrew-d.github.io/python-multipart/" target="_blank"><code>python-multipart</code></a> - Required if you want to support form <abbr title="converting the string that comes from an HTTP request into Python data">"parsing"</abbr>, with `request.form()`.
448448
* <a href="https://pythonhosted.org/itsdangerous/" target="_blank"><code>itsdangerous</code></a> - Required for `SessionMiddleware` support.
449449
* <a href="https://pyyaml.org/wiki/PyYAMLDocumentation" target="_blank"><code>pyyaml</code></a> - Required for Starlette's `SchemaGenerator` support (you probably don't need it with FastAPI).
450-
* <a href="https://graphene-python.org/" target="_blank"><code>graphene</code></a> - Required for `GraphQLApp` support.
451450
* <a href="https://github.com/esnme/ultrajson" target="_blank"><code>ujson</code></a> - Required if you want to use `UJSONResponse`.
452451

453452
Used by FastAPI / Starlette:

docs/de/docs/features.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@ Mit **FastAPI** bekommen Sie viele von **Starlette**'s Funktionen (da FastAPI nu
167167

168168
* Stark beeindruckende Performanz. Es ist <a href="https://github.com/encode/starlette#performance" class="external-link" target="_blank">eines der schnellsten Python frameworks, auf Augenhöhe mit **NodeJS** und **Go**</a>.
169169
* **WebSocket**-Unterstützung.
170-
* **GraphQL**-Unterstützung.
171170
* Hintergrundaufgaben im selben Prozess.
172171
* Ereignisse für das Starten und Herunterfahren.
173172
* Testclient basierend auf `requests`.

docs/de/docs/index.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,6 @@ For a more complete example including more features, see the <a href="https://fa
425425
* More advanced (but equally easy) techniques for declaring **deeply nested JSON models** (thanks to Pydantic).
426426
* Many extra features (thanks to Starlette) as:
427427
* **WebSockets**
428-
* **GraphQL**
429428
* extremely easy tests based on `requests` and `pytest`
430429
* **CORS**
431430
* **Cookie Sessions**
@@ -452,7 +451,6 @@ Used by Starlette:
452451
* <a href="https://andrew-d.github.io/python-multipart/" target="_blank"><code>python-multipart</code></a> - Required if you want to support form <abbr title="converting the string that comes from an HTTP request into Python data">"parsing"</abbr>, with `request.form()`.
453452
* <a href="https://pythonhosted.org/itsdangerous/" target="_blank"><code>itsdangerous</code></a> - Required for `SessionMiddleware` support.
454453
* <a href="https://pyyaml.org/wiki/PyYAMLDocumentation" target="_blank"><code>pyyaml</code></a> - Required for Starlette's `SchemaGenerator` support (you probably don't need it with FastAPI).
455-
* <a href="https://graphene-python.org/" target="_blank"><code>graphene</code></a> - Required for `GraphQLApp` support.
456454
* <a href="https://github.com/esnme/ultrajson" target="_blank"><code>ujson</code></a> - Required if you want to use `UJSONResponse`.
457455

458456
Used by FastAPI / Starlette:

docs/en/docs/advanced/graphql.md

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,56 @@
11
# GraphQL
22

3-
**FastAPI** has optional support for GraphQL (provided by Starlette directly), using the `graphene` library.
3+
As **FastAPI** is based on the **ASGI** standard, it's very easy to integrate any **GraphQL** library also compatible with ASGI.
44

55
You can combine normal FastAPI *path operations* with GraphQL on the same application.
66

7-
## Import and use `graphene`
7+
!!! tip
8+
**GraphQL** solves some very specific use cases.
89

9-
GraphQL is implemented with Graphene, you can check <a href="https://docs.graphene-python.org/en/latest/quickstart/" class="external-link" target="_blank">Graphene's docs</a> for more details.
10+
It has **advantages** and **disadvantages** when compared to common **web APIs**.
1011

11-
Import `graphene` and define your GraphQL data:
12+
Make sure you evaluate if the **benefits** for your use case compensate the **drawbacks**. 🤓
1213

13-
```Python hl_lines="1 6-10"
14-
{!../../../docs_src/graphql/tutorial001.py!}
15-
```
14+
## GraphQL Libraries
15+
16+
Here are some of the **GraphQL** libraries that have **ASGI** support. You could use them with **FastAPI**:
17+
18+
* <a href="https://strawberry.rocks/" class="external-link" target="_blank">Strawberry</a> 🍓
19+
* With <a href="https://strawberry.rocks/docs/integrations/fastapi" class="external-link" target="_blank">docs for FastAPI</a>
20+
* <a href="https://ariadnegraphql.org/" class="external-link" target="_blank">Ariadne</a>
21+
* With <a href="https://ariadnegraphql.org/docs/starlette-integration" class="external-link" target="_blank">docs for Starlette</a> (that also apply to FastAPI)
22+
* <a href="https://tartiflette.io/" class="external-link" target="_blank">Tartiflette</a>
23+
* With <a href="https://tartiflette.github.io/tartiflette-asgi/" class="external-link" target="_blank">Tartiflette ASGI</a> to provide ASGI integration
24+
* <a href="https://graphene-python.org/" class="external-link" target="_blank">Graphene</a>
25+
* With <a href="https://github.com/ciscorn/starlette-graphene3" class="external-link" target="_blank">starlette-graphene3</a>
26+
27+
## GraphQL with Strawberry
28+
29+
If you need or want to work with **GraphQL**, <a href="https://strawberry.rocks/" class="external-link" target="_blank">**Strawberry**</a> is the **recommended** library as it has the design closest to **FastAPI's** design, it's all based on **type annotations**.
1630

17-
## Add Starlette's `GraphQLApp`
31+
Depending on your use case, you might prefer to use a different library, but if you asked me, I would probably suggest you try **Strawberry**.
1832

19-
Then import and add Starlette's `GraphQLApp`:
33+
Here's a small preview of how you could integrate Strawberry with FastAPI:
2034

21-
```Python hl_lines="3 14"
35+
```Python hl_lines="3 22 25-26"
2236
{!../../../docs_src/graphql/tutorial001.py!}
2337
```
2438

25-
!!! info
26-
Here we are using `.add_route`, that is the way to add a route in Starlette (inherited by FastAPI) without declaring the specific operation (as would be with `.get()`, `.post()`, etc).
39+
You can learn more about Strawberry in the <a href="https://strawberry.rocks/" class="external-link" target="_blank">Strawberry documentation</a>.
2740

28-
## Check it
41+
And also the docs about <a href="https://strawberry.rocks/docs/integrations/fastapi" class="external-link" target="_blank">Strawberry with FastAPI</a>.
2942

30-
Run it with Uvicorn and open your browser at <a href="http://127.0.0.1:8000" class="external-link" target="_blank">http://127.0.0.1:8000</a>.
43+
## Older `GraphQLApp` from Starlette
3144

32-
You will see GraphiQL web user interface:
45+
Previous versions of Starlette included a `GraphQLApp` class to integrate with <a href="https://graphene-python.org/" class="external-link" target="_blank">Graphene</a>.
3346

34-
<img src="/img/tutorial/graphql/image01.png">
47+
It was deprecated from Starlette, but if you have code that used it, you can easily **migrate** to <a href="https://github.com/ciscorn/starlette-graphene3" class="external-link" target="_blank">starlette-graphene3</a>, that covers the same use case and has an **almost identical interface**.
3548

36-
## More details
49+
!!! tip
50+
If you need GraphQL, I still would recommend you check out <a href="https://strawberry.rocks/" class="external-link" target="_blank">Strawberry</a>, as it's based on type annotations instead of custom classes and types.
3751

38-
For more details, including:
52+
## Learn More
3953

40-
* Accessing request information
41-
* Adding background tasks
42-
* Using normal or async functions
54+
You can learn more about **GraphQL** in the <a href="https://graphql.org/" class="external-link" target="_blank">official GraphQL documentation</a>.
4355

44-
check the official <a href="https://www.starlette.io/graphql/" class="external-link" target="_blank">Starlette GraphQL docs</a>.
56+
You can also read more about each those libraries described above in their links.

docs/en/docs/alternatives.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,15 +367,14 @@ It has:
367367

368368
* Seriously impressive performance.
369369
* WebSocket support.
370-
* GraphQL support.
371370
* In-process background tasks.
372371
* Startup and shutdown events.
373372
* Test client built on requests.
374373
* CORS, GZip, Static Files, Streaming responses.
375374
* Session and Cookie support.
376375
* 100% test coverage.
377376
* 100% type annotated codebase.
378-
* Zero hard dependencies.
377+
* Few hard dependencies.
379378

380379
Starlette is currently the fastest Python framework tested. Only surpassed by Uvicorn, which is not a framework, but a server.
381380

docs/en/docs/features.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ With **FastAPI** you get all of **Starlette**'s features (as FastAPI is just Sta
164164

165165
* Seriously impressive performance. It is <a href="https://github.com/encode/starlette#performance" class="external-link" target="_blank">one of the fastest Python frameworks available, on par with **NodeJS** and **Go**</a>.
166166
* **WebSocket** support.
167-
* **GraphQL** support.
168167
* In-process background tasks.
169168
* Startup and shutdown events.
170169
* Test client built on `requests`.

docs/en/docs/index.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,9 +419,9 @@ For a more complete example including more features, see the <a href="https://fa
419419
* A very powerful and easy to use **<abbr title="also known as components, resources, providers, services, injectables">Dependency Injection</abbr>** system.
420420
* Security and authentication, including support for **OAuth2** with **JWT tokens** and **HTTP Basic** auth.
421421
* More advanced (but equally easy) techniques for declaring **deeply nested JSON models** (thanks to Pydantic).
422+
* **GraphQL** integration with <a href="https://strawberry.rocks" class="external-link" target="_blank">Strawberry</a> and other libraries.
422423
* Many extra features (thanks to Starlette) as:
423424
* **WebSockets**
424-
* **GraphQL**
425425
* extremely easy tests based on `requests` and `pytest`
426426
* **CORS**
427427
* **Cookie Sessions**
@@ -448,7 +448,6 @@ Used by Starlette:
448448
* <a href="https://andrew-d.github.io/python-multipart/" target="_blank"><code>python-multipart</code></a> - Required if you want to support form <abbr title="converting the string that comes from an HTTP request into Python data">"parsing"</abbr>, with `request.form()`.
449449
* <a href="https://pythonhosted.org/itsdangerous/" target="_blank"><code>itsdangerous</code></a> - Required for `SessionMiddleware` support.
450450
* <a href="https://pyyaml.org/wiki/PyYAMLDocumentation" target="_blank"><code>pyyaml</code></a> - Required for Starlette's `SchemaGenerator` support (you probably don't need it with FastAPI).
451-
* <a href="https://graphene-python.org/" target="_blank"><code>graphene</code></a> - Required for `GraphQLApp` support.
452451
* <a href="https://github.com/esnme/ultrajson" target="_blank"><code>ujson</code></a> - Required if you want to use `UJSONResponse`.
453452

454453
Used by FastAPI / Starlette:

docs_src/graphql/tutorial001.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
1-
import graphene
1+
import strawberry
22
from fastapi import FastAPI
3-
from starlette.graphql import GraphQLApp
3+
from strawberry.asgi import GraphQL
44

55

6-
class Query(graphene.ObjectType):
7-
hello = graphene.String(name=graphene.String(default_value="stranger"))
6+
@strawberry.type
7+
class User:
8+
name: str
9+
age: int
810

9-
def resolve_hello(self, info, name):
10-
return "Hello " + name
1111

12+
@strawberry.type
13+
class Query:
14+
@strawberry.field
15+
def user(self) -> User:
16+
return User(name="Patrick", age=100)
17+
18+
19+
schema = strawberry.Schema(query=Query)
20+
21+
22+
graphql_app = GraphQL(schema)
1223

1324
app = FastAPI()
14-
app.add_route("/", GraphQLApp(schema=graphene.Schema(query=Query)))
25+
app.add_route("/graphql", graphql_app)
26+
app.add_websocket_route("/graphql", graphql_app)

0 commit comments

Comments
 (0)