diff --git a/README.md b/README.md index 233e8ed57b7b8..5bdd9e6dbbce0 100644 --- a/README.md +++ b/README.md @@ -418,9 +418,9 @@ For a more complete example including more features, see the Dependency Injection** system. * Security and authentication, including support for **OAuth2** with **JWT tokens** and **HTTP Basic** auth. * More advanced (but equally easy) techniques for declaring **deeply nested JSON models** (thanks to Pydantic). +* **GraphQL** integration with Strawberry and other libraries. * Many extra features (thanks to Starlette) as: * **WebSockets** - * **GraphQL** * extremely easy tests based on `requests` and `pytest` * **CORS** * **Cookie Sessions** @@ -447,7 +447,6 @@ Used by Starlette: * python-multipart - Required if you want to support form "parsing", with `request.form()`. * itsdangerous - Required for `SessionMiddleware` support. * pyyaml - Required for Starlette's `SchemaGenerator` support (you probably don't need it with FastAPI). -* graphene - Required for `GraphQLApp` support. * ujson - Required if you want to use `UJSONResponse`. Used by FastAPI / Starlette: diff --git a/docs/de/docs/features.md b/docs/de/docs/features.md index e5b38616f47a4..f56257e7e2d30 100644 --- a/docs/de/docs/features.md +++ b/docs/de/docs/features.md @@ -167,7 +167,6 @@ Mit **FastAPI** bekommen Sie viele von **Starlette**'s Funktionen (da FastAPI nu * Stark beeindruckende Performanz. Es ist eines der schnellsten Python frameworks, auf Augenhöhe mit **NodeJS** und **Go**. * **WebSocket**-Unterstützung. -* **GraphQL**-Unterstützung. * Hintergrundaufgaben im selben Prozess. * Ereignisse für das Starten und Herunterfahren. * Testclient basierend auf `requests`. diff --git a/docs/de/docs/index.md b/docs/de/docs/index.md index 95fb7ae212518..d09ce70a097d6 100644 --- a/docs/de/docs/index.md +++ b/docs/de/docs/index.md @@ -425,7 +425,6 @@ For a more complete example including more features, see the python-multipart - Required if you want to support form "parsing", with `request.form()`. * itsdangerous - Required for `SessionMiddleware` support. * pyyaml - Required for Starlette's `SchemaGenerator` support (you probably don't need it with FastAPI). -* graphene - Required for `GraphQLApp` support. * ujson - Required if you want to use `UJSONResponse`. Used by FastAPI / Starlette: diff --git a/docs/en/docs/advanced/graphql.md b/docs/en/docs/advanced/graphql.md index acc1a39f26ba8..154606406a6a0 100644 --- a/docs/en/docs/advanced/graphql.md +++ b/docs/en/docs/advanced/graphql.md @@ -1,44 +1,56 @@ # GraphQL -**FastAPI** has optional support for GraphQL (provided by Starlette directly), using the `graphene` library. +As **FastAPI** is based on the **ASGI** standard, it's very easy to integrate any **GraphQL** library also compatible with ASGI. You can combine normal FastAPI *path operations* with GraphQL on the same application. -## Import and use `graphene` +!!! tip + **GraphQL** solves some very specific use cases. -GraphQL is implemented with Graphene, you can check Graphene's docs for more details. + It has **advantages** and **disadvantages** when compared to common **web APIs**. -Import `graphene` and define your GraphQL data: + Make sure you evaluate if the **benefits** for your use case compensate the **drawbacks**. 🤓 -```Python hl_lines="1 6-10" -{!../../../docs_src/graphql/tutorial001.py!} -``` +## GraphQL Libraries + +Here are some of the **GraphQL** libraries that have **ASGI** support. You could use them with **FastAPI**: + +* Strawberry 🍓 + * With docs for FastAPI +* Ariadne + * With docs for Starlette (that also apply to FastAPI) +* Tartiflette + * With Tartiflette ASGI to provide ASGI integration +* Graphene + * With starlette-graphene3 + +## GraphQL with Strawberry + +If you need or want to work with **GraphQL**, **Strawberry** is the **recommended** library as it has the design closest to **FastAPI's** design, it's all based on **type annotations**. -## Add Starlette's `GraphQLApp` +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**. -Then import and add Starlette's `GraphQLApp`: +Here's a small preview of how you could integrate Strawberry with FastAPI: -```Python hl_lines="3 14" +```Python hl_lines="3 22 25-26" {!../../../docs_src/graphql/tutorial001.py!} ``` -!!! info - 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). +You can learn more about Strawberry in the Strawberry documentation. -## Check it +And also the docs about Strawberry with FastAPI. -Run it with Uvicorn and open your browser at http://127.0.0.1:8000. +## Older `GraphQLApp` from Starlette -You will see GraphiQL web user interface: +Previous versions of Starlette included a `GraphQLApp` class to integrate with Graphene. - +It was deprecated from Starlette, but if you have code that used it, you can easily **migrate** to starlette-graphene3, that covers the same use case and has an **almost identical interface**. -## More details +!!! tip + If you need GraphQL, I still would recommend you check out Strawberry, as it's based on type annotations instead of custom classes and types. -For more details, including: +## Learn More -* Accessing request information -* Adding background tasks -* Using normal or async functions +You can learn more about **GraphQL** in the official GraphQL documentation. -check the official Starlette GraphQL docs. +You can also read more about each those libraries described above in their links. diff --git a/docs/en/docs/alternatives.md b/docs/en/docs/alternatives.md index 8b1fa7786d68a..d2792eb0cb613 100644 --- a/docs/en/docs/alternatives.md +++ b/docs/en/docs/alternatives.md @@ -367,7 +367,6 @@ It has: * Seriously impressive performance. * WebSocket support. -* GraphQL support. * In-process background tasks. * Startup and shutdown events. * Test client built on requests. @@ -375,7 +374,7 @@ It has: * Session and Cookie support. * 100% test coverage. * 100% type annotated codebase. -* Zero hard dependencies. +* Few hard dependencies. Starlette is currently the fastest Python framework tested. Only surpassed by Uvicorn, which is not a framework, but a server. diff --git a/docs/en/docs/features.md b/docs/en/docs/features.md index c92795d4a7ced..36f80783a6104 100644 --- a/docs/en/docs/features.md +++ b/docs/en/docs/features.md @@ -164,7 +164,6 @@ With **FastAPI** you get all of **Starlette**'s features (as FastAPI is just Sta * Seriously impressive performance. It is one of the fastest Python frameworks available, on par with **NodeJS** and **Go**. * **WebSocket** support. -* **GraphQL** support. * In-process background tasks. * Startup and shutdown events. * Test client built on `requests`. diff --git a/docs/en/docs/index.md b/docs/en/docs/index.md index 33aadb8ef656a..ef593153fa857 100644 --- a/docs/en/docs/index.md +++ b/docs/en/docs/index.md @@ -419,9 +419,9 @@ For a more complete example including more features, see the Dependency Injection** system. * Security and authentication, including support for **OAuth2** with **JWT tokens** and **HTTP Basic** auth. * More advanced (but equally easy) techniques for declaring **deeply nested JSON models** (thanks to Pydantic). +* **GraphQL** integration with Strawberry and other libraries. * Many extra features (thanks to Starlette) as: * **WebSockets** - * **GraphQL** * extremely easy tests based on `requests` and `pytest` * **CORS** * **Cookie Sessions** @@ -448,7 +448,6 @@ Used by Starlette: * python-multipart - Required if you want to support form "parsing", with `request.form()`. * itsdangerous - Required for `SessionMiddleware` support. * pyyaml - Required for Starlette's `SchemaGenerator` support (you probably don't need it with FastAPI). -* graphene - Required for `GraphQLApp` support. * ujson - Required if you want to use `UJSONResponse`. Used by FastAPI / Starlette: diff --git a/docs_src/graphql/tutorial001.py b/docs_src/graphql/tutorial001.py index 41da361d3028b..3b4ca99cf6b50 100644 --- a/docs_src/graphql/tutorial001.py +++ b/docs_src/graphql/tutorial001.py @@ -1,14 +1,26 @@ -import graphene +import strawberry from fastapi import FastAPI -from starlette.graphql import GraphQLApp +from strawberry.asgi import GraphQL -class Query(graphene.ObjectType): - hello = graphene.String(name=graphene.String(default_value="stranger")) +@strawberry.type +class User: + name: str + age: int - def resolve_hello(self, info, name): - return "Hello " + name +@strawberry.type +class Query: + @strawberry.field + def user(self) -> User: + return User(name="Patrick", age=100) + + +schema = strawberry.Schema(query=Query) + + +graphql_app = GraphQL(schema) app = FastAPI() -app.add_route("/", GraphQLApp(schema=graphene.Schema(query=Query))) +app.add_route("/graphql", graphql_app) +app.add_websocket_route("/graphql", graphql_app)