-
-
Notifications
You must be signed in to change notification settings - Fork 511
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reimplement channel_listen as context manager (#2856)
Co-authored-by: Jonathan Ehwald <github@ehwald.info> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
29baae0
commit c18be1f
Showing
5 changed files
with
479 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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 |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| Release type: minor | ||
|
|
||
| This release updates the API to listen to Django Channels to avoid race conditions | ||
| when confirming GraphQL subscriptions. | ||
|
|
||
| **Deprecations:** | ||
|
|
||
| This release contains a deprecation for the Channels integration. The `channel_listen` | ||
| method will be replaced with an async context manager that returns an awaitable | ||
| AsyncGenerator. This method is called `listen_to_channel`. | ||
|
|
||
| An example of migrating existing code is given below: | ||
|
|
||
| ```py | ||
| # Existing code | ||
| @strawberry.type | ||
| class MyDataType: | ||
| name: str | ||
|
|
||
| @strawberry.type | ||
| class Subscription: | ||
| @strawberry.subscription | ||
| async def my_data_subscription( | ||
| self, info: Info, groups: list[str] | ||
| ) -> AsyncGenerator[MyDataType | None, None]: | ||
| yield None | ||
| async for message in info.context["ws"].channel_listen("my_data", groups=groups): | ||
| yield MyDataType(name=message["payload"]) | ||
| ``` | ||
|
|
||
| ```py | ||
| # New code | ||
| @strawberry.type | ||
| class Subscription: | ||
| @strawberry.subscription | ||
| async def my_data_subscription( | ||
| self, info: Info, groups: list[str] | ||
| ) -> AsyncGenerator[MyDataType | None, None]: | ||
| async with info.context["ws"].listen_to_channel("my_data", groups=groups) as cm: | ||
| yield None | ||
| async for message in cm: | ||
| yield MyDataType(name=message["payload"]) | ||
| ``` |
This file contains 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 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.