mirror of https://github.com/tiangolo/fastapi.git
✨ Add docs for GraphQL (#48)
This commit is contained in:
parent
bf53518141
commit
984dd71d13
Binary file not shown.
|
After Width: | Height: | Size: 93 KiB |
|
|
@ -0,0 +1,14 @@
|
||||||
|
import graphene
|
||||||
|
from fastapi import FastAPI
|
||||||
|
from starlette.graphql import GraphQLApp
|
||||||
|
|
||||||
|
|
||||||
|
class Query(graphene.ObjectType):
|
||||||
|
hello = graphene.String(name=graphene.String(default_value="stranger"))
|
||||||
|
|
||||||
|
def resolve_hello(self, info, name):
|
||||||
|
return "Hello " + name
|
||||||
|
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
app.add_route("/", GraphQLApp(schema=graphene.Schema(query=Query)))
|
||||||
|
|
@ -0,0 +1,44 @@
|
||||||
|
|
||||||
|
**FastAPI** has optional support for GraphQL (provided by Starlette directly), using the `graphene` library.
|
||||||
|
|
||||||
|
You can combine normal FastAPI path operations with GraphQL on the same application.
|
||||||
|
|
||||||
|
## Import and use `graphene`
|
||||||
|
|
||||||
|
GraphQL is implemented with Graphene, you can check <a href="https://docs.graphene-python.org/en/latest/quickstart/" target="_blank">Graphene's docs</a> for more details.
|
||||||
|
|
||||||
|
Import `graphene` and define your GraphQL data:
|
||||||
|
|
||||||
|
```Python hl_lines="1 6 7 8 9 10"
|
||||||
|
{!./src/graphql/tutorial001.py!}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Add Starlette's `GraphQLApp`
|
||||||
|
|
||||||
|
Then import and add Starlette's `GraphQLApp`:
|
||||||
|
|
||||||
|
```Python hl_lines="3 14"
|
||||||
|
{!./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).
|
||||||
|
|
||||||
|
## Check it
|
||||||
|
|
||||||
|
Run it with Uvicorn and open your browser at <a href="http://127.0.0.1:8000" target="_blank">http://127.0.0.1:8000</a>.
|
||||||
|
|
||||||
|
You will see GraphiQL web user interface:
|
||||||
|
|
||||||
|
<img src="/img/tutorial/graphql/image01.png">
|
||||||
|
|
||||||
|
|
||||||
|
## More details
|
||||||
|
|
||||||
|
For more details, including:
|
||||||
|
|
||||||
|
* Accessing request information
|
||||||
|
* Adding background tasks
|
||||||
|
* Using normal or async functions
|
||||||
|
|
||||||
|
check the official <a href="https://www.starlette.io/graphql/" target="_blank">Starlette GraphQL docs</a>.
|
||||||
|
|
@ -61,6 +61,7 @@ nav:
|
||||||
- Bigger Applications - Multiple Files: 'tutorial/bigger-applications.md'
|
- Bigger Applications - Multiple Files: 'tutorial/bigger-applications.md'
|
||||||
- Sub Applications - Behind a Proxy: 'tutorial/sub-applications-proxy.md'
|
- Sub Applications - Behind a Proxy: 'tutorial/sub-applications-proxy.md'
|
||||||
- Application Configuration: 'tutorial/application-configuration.md'
|
- Application Configuration: 'tutorial/application-configuration.md'
|
||||||
|
- GraphQL: 'tutorial/graphq.md'
|
||||||
- Concurrency and async / await: 'async.md'
|
- Concurrency and async / await: 'async.md'
|
||||||
- Deployment: 'deployment.md'
|
- Deployment: 'deployment.md'
|
||||||
- Project Generation - Template: 'project-generation.md'
|
- Project Generation - Template: 'project-generation.md'
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue