-
-
Notifications
You must be signed in to change notification settings - Fork 1k
🔥 Remove GraphQL support #1198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
🔥 Remove GraphQL support #1198
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ceee483
🔥 Remove GraphQL support
Kludex ad29baa
Merge master
Kludex 48bc30d
Remove graphene dependency and add docs
Kludex bd9ed14
Update docs/graphql.md
Kludex 92568bd
Update docs/graphql.md
Kludex 850c455
Remove aiofiles warning on setup.cfg
Kludex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,116 +1,9 @@ | ||
GraphQL support in Starlette was deprecated in version 0.15.0, and removed in version 0.17.0. | ||
|
||
!!! Warning | ||
Although GraphQL support is no longer built in to Starlette, you can still use GraphQL with Starlette via 3rd party libraries. These libraries all have Starlette-specific guides to help you do just that: | ||
|
||
GraphQL support in Starlette is **deprecated** as of version 0.15 and will | ||
be removed in a future release. It is also incompatible with Python 3.10+. | ||
Please consider using a third-party library to provide GraphQL support. This | ||
is usually done by mounting a GraphQL ASGI application. | ||
See [#619](https://github.com/encode/starlette/issues/619). | ||
Some example libraries are: | ||
- [Ariadne](https://ariadnegraphql.org/docs/starlette-integration.html) | ||
- [`starlette-graphene3`](https://github.com/ciscorn/starlette-graphene3#example) | ||
- [Strawberry](https://strawberry.rocks/docs/integrations/starlette) | ||
- [`tartiflette-asgi`](https://tartiflette.github.io/tartiflette-asgi/usage/#starlette) | ||
|
||
* [Ariadne](https://ariadnegraphql.org/docs/asgi) | ||
* [`tartiflette-asgi`](https://tartiflette.github.io/tartiflette-asgi/) | ||
* [Strawberry](https://strawberry.rocks/docs/integrations/asgi) | ||
* [`starlette-graphene3`](https://github.com/ciscorn/starlette-graphene3) | ||
|
||
Starlette includes optional support for GraphQL, using the `graphene` library. | ||
|
||
Here's an example of integrating the support into your application. | ||
|
||
```python | ||
from starlette.applications import Starlette | ||
from starlette.routing import Route | ||
from starlette.graphql import GraphQLApp | ||
import graphene | ||
|
||
|
||
class Query(graphene.ObjectType): | ||
hello = graphene.String(name=graphene.String(default_value="stranger")) | ||
|
||
def resolve_hello(self, info, name): | ||
return "Hello " + name | ||
|
||
routes = [ | ||
Route('/', GraphQLApp(schema=graphene.Schema(query=Query))) | ||
] | ||
|
||
app = Starlette(routes=routes) | ||
``` | ||
|
||
If you load up the page in a browser, you'll be served the GraphiQL tool, | ||
which you can use to interact with your GraphQL API. | ||
|
||
|
||
 | ||
|
||
## Accessing request information | ||
|
||
The current request is available in the context. | ||
|
||
```python | ||
class Query(graphene.ObjectType): | ||
user_agent = graphene.String() | ||
|
||
def resolve_user_agent(self, info): | ||
""" | ||
Return the User-Agent of the incoming request. | ||
""" | ||
request = info.context["request"] | ||
return request.headers.get("User-Agent", "<unknown>") | ||
``` | ||
|
||
## Adding background tasks | ||
|
||
You can add background tasks to run once the response has been sent. | ||
|
||
```python | ||
class Query(graphene.ObjectType): | ||
user_agent = graphene.String() | ||
|
||
def resolve_user_agent(self, info): | ||
""" | ||
Return the User-Agent of the incoming request. | ||
""" | ||
user_agent = request.headers.get("User-Agent", "<unknown>") | ||
background = info.context["background"] | ||
background.add_task(log_user_agent, user_agent=user_agent) | ||
return user_agent | ||
|
||
async def log_user_agent(user_agent): | ||
... | ||
``` | ||
|
||
## Sync or Async executors | ||
|
||
If you're working with a standard ORM, then just use regular function calls for | ||
your "resolve" methods, and Starlette will manage running the GraphQL query within a | ||
separate thread. | ||
|
||
If you want to use an asynchronous ORM, then use "async resolve" methods, and | ||
make sure to setup Graphene's AsyncioExecutor using the `executor` argument. | ||
|
||
```python | ||
from graphql.execution.executors.asyncio import AsyncioExecutor | ||
from starlette.applications import Starlette | ||
from starlette.graphql import GraphQLApp | ||
from starlette.routing import Route | ||
import graphene | ||
|
||
|
||
class Query(graphene.ObjectType): | ||
hello = graphene.String(name=graphene.String(default_value="stranger")) | ||
|
||
async def resolve_hello(self, info, name): | ||
# We can make asynchronous network calls here. | ||
return "Hello " + name | ||
|
||
routes = [ | ||
# We're using `executor_class=AsyncioExecutor` here. | ||
Route('/', GraphQLApp( | ||
schema=graphene.Schema(query=Query), | ||
executor_class=AsyncioExecutor | ||
)) | ||
] | ||
|
||
app = Starlette(routes=routes) | ||
``` |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
# Optionals | ||
graphene; python_version<'3.10' | ||
itsdangerous | ||
jinja2 | ||
python-multipart | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.