|
1 | 1 | # GraphQL |
2 | 2 |
|
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. |
4 | 4 |
|
5 | 5 | You can combine normal FastAPI *path operations* with GraphQL on the same application. |
6 | 6 |
|
7 | | -## Import and use `graphene` |
| 7 | +!!! tip |
| 8 | + **GraphQL** solves some very specific use cases. |
8 | 9 |
|
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**. |
10 | 11 |
|
11 | | -Import `graphene` and define your GraphQL data: |
| 12 | + Make sure you evaluate if the **benefits** for your use case compensate the **drawbacks**. 🤓 |
12 | 13 |
|
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**. |
16 | 30 |
|
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**. |
18 | 32 |
|
19 | | -Then import and add Starlette's `GraphQLApp`: |
| 33 | +Here's a small preview of how you could integrate Strawberry with FastAPI: |
20 | 34 |
|
21 | | -```Python hl_lines="3 14" |
| 35 | +```Python hl_lines="3 22 25-26" |
22 | 36 | {!../../../docs_src/graphql/tutorial001.py!} |
23 | 37 | ``` |
24 | 38 |
|
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>. |
27 | 40 |
|
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>. |
29 | 42 |
|
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 |
31 | 44 |
|
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>. |
33 | 46 |
|
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**. |
35 | 48 |
|
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. |
37 | 51 |
|
38 | | -For more details, including: |
| 52 | +## Learn More |
39 | 53 |
|
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>. |
43 | 55 |
|
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. |
0 commit comments