Skip to content
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

Docs improvement Subscriptions #376

Merged
merged 38 commits into from
Oct 11, 2023
Merged
Changes from 5 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
9925103
write down how to run an asgi test-server
Sep 26, 2023
3b8550c
adding the runserver_asgi command, referred to in the docs
Sep 26, 2023
d303235
Relying on django integrated daphne instead of re-inventing the wheel
Sep 27, 2023
a58fd45
show end result
Sep 27, 2023
01314b2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 27, 2023
d8fe53d
Update docs/guide/subscriptions.md
sdobbelaere Sep 29, 2023
d92e90f
Update docs/guide/subscriptions.md
sdobbelaere Sep 29, 2023
d6b7361
Update docs/guide/subscriptions.md
sdobbelaere Sep 29, 2023
ccd0dc8
Update docs/guide/subscriptions.md
sdobbelaere Sep 29, 2023
fd873be
Centralise get_current_user DRY, add new router that is Auth enabled …
Sep 29, 2023
3e5091f
Merge conlifcts
Sep 29, 2023
4a983de
Refactoring to explain AGSI support better, and include the new Auth …
Sep 29, 2023
66869df
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 29, 2023
bc4d11a
Merge branch 'strawberry-graphql:main' into subscriptions
sdobbelaere Sep 29, 2023
5de8f89
Update docs/guide/subscriptions.md
sdobbelaere Sep 30, 2023
2f34eed
Update strawberry_django/auth/queries.py
sdobbelaere Sep 30, 2023
cbd154e
Update tests/projects/schema.py
sdobbelaere Sep 30, 2023
346c8dc
Applying suggestions
Sep 30, 2023
ab9bb7b
remove baseuser imports
Sep 30, 2023
17281ea
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 30, 2023
6600d5c
force user object loading for async
Sep 30, 2023
3d7dc69
Merge branch 'subscriptions' of github.com:sdobbelaere/strawberry-gra…
Sep 30, 2023
d406c72
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 30, 2023
7e361bd
Update strawberry_django/auth/utils.py
sdobbelaere Sep 30, 2023
79caaef
styling
Oct 1, 2023
1cccdd4
merge conflicts
Oct 1, 2023
1d8399d
Adding return types
Oct 2, 2023
d28b9df
adjust docs to reflect new import path
Oct 2, 2023
597e7af
remove typing to ensure correct import
Oct 2, 2023
05ab688
Merge branch 'main' into subscriptions
Oct 3, 2023
6d51db2
Merge remote-tracking branch 'origin/main' into subscriptions
bellini666 Oct 9, 2023
f1e1b62
refactor: improve get_current_user typing
bellini666 Oct 9, 2023
e5d775f
fix(pyright): fix remaining typing issues
bellini666 Oct 9, 2023
b0a464a
allow for accing a fake context in order to run tests
Oct 9, 2023
e08e0bb
Merge branch 'subscriptions' of github.com:sdobbelaere/strawberry-gra…
Oct 9, 2023
d3d70b3
Improve fording to satisfy pre-commit alex
Oct 9, 2023
1bcbb67
revert context override, and fix docs
Oct 9, 2023
9deaffb
Fix indentation and historical typo
Oct 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 132 additions & 2 deletions docs/guide/subscriptions.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,136 @@
### Subscriptions
# Subscriptions

Subscriptions are supported using the
[Strawberry Django Channels](https://strawberry.rocks/docs/integrations/channels) integration.

Check its docs to know how to use it.
This guide will give you a minimal working example to get you going.
There are 3 parts to this guide:

1. Making Django compatible
2. Setup local testing
3. Creating your first subscription

## Making Django compatible

It's important to realise that Django doesnt support websockets out of the box.
sdobbelaere marked this conversation as resolved.
Show resolved Hide resolved
To resolve this, we can help the platform along a little.

Edit your `MyProject.asgi.py` file and replace it with the following content.
Ensure that you replace the relevant code with your setup.
sdobbelaere marked this conversation as resolved.
Show resolved Hide resolved

```python
# MyProject.asgi.py
import os

from django.core.asgi import get_asgi_application
from strawberry.channels import GraphQLProtocolTypeRouter

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MyProject.settings") # CHANGE the project name
django_asgi_app = get_asgi_application()

# Import your Strawberry schema after creating the django ASGI application
# This ensures django.setup() has been called before any ORM models are imported
# for the schema.

from .schema import schema # CHANGE path to where you housed your schema file.


application = GraphQLProtocolTypeRouter(
schema,
django_application=django_asgi_app,
)
```

Also, ensure that you enable subscriptions on your AsgiGraphQLView in `MyProject.urls.py`:

```python
...

urlpatterns = [
...
path('graphql/', AsyncGraphQLView.as_view(
schema=schema,
graphiql=settings.DEBUG,
subscriptions_enabled=True
)
),
sdobbelaere marked this conversation as resolved.
Show resolved Hide resolved
...
]

```

Note, django-channels allows for a lot more complexity. Here we just cover the basic framework to get
sdobbelaere marked this conversation as resolved.
Show resolved Hide resolved
subscriptions to run on Django with minimal effort.

## Setup local testing

The classic `./manage.py runserver` will not support subscriptions. However, Django has daphne support out of the box to ensure that we can actually use Daphne for the runserver command. [Source](https://docs.djangoproject.com/en/4.2/howto/deployment/asgi/daphne/)
sdobbelaere marked this conversation as resolved.
Show resolved Hide resolved

Firstly, we need install Daphne to handle the workload, so let's install it:

```bash
pip install daphne
```

Secondly, we need to add `daphne` to your settings.py file before 'django.contrib.staticfiles'

```python
INSTALLED_APPS = [
...
'daphne',
'django.contrib.staticfiles',
...
]
```

and add your ASGI_APPLICATION setting in your settings.py
sdobbelaere marked this conversation as resolved.
Show resolved Hide resolved

```python
# settings.py
...
ASGI_APPLICATION = 'MyProject.asgi.application'
...
```

Now you can run your test-server like as usual:

```bash
./manage.py runserver
```

## Creating your first subscription

Once you've taken care of those 2 setup steps, your first subscription is a breeze.
Go and edit your schema-file and add:

```python
from strawberry import type, subscription
import asyncio

@type
class Subscription:
@subscription
sdobbelaere marked this conversation as resolved.
Show resolved Hide resolved
async def count(self, target: int = 100) -> int:
for i in range(target):
yield i
await asyncio.sleep(0.5)
```

That's pretty much it for this basic start.
See for yourself by running your test server `./manange.py runserver` and opening `http://127.0.0.1:8000/graphql/` in your browser. Now run:

```graphql
subscription {
count(target: 10)
}
```

You should see something like (where the count changes every .5s to a max of 9)

```json
{
"data": {
"count": 9
}
}
```