Skip to content

Commit c9cda69

Browse files
committed
Update and simplify the channels2 example
1 parent d365c72 commit c9cda69

File tree

9 files changed

+28
-163
lines changed

9 files changed

+28
-163
lines changed

README.md

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,13 @@ channel_routing = [
208208
Set up with Django Channels just takes three steps:
209209

210210
1. Install the apps
211-
2. Set up schema
212-
3. Set up channels Router
211+
2. Set up your schema
212+
3. Configure the channels router application
213213

214214

215215
First `pip install channels` and it to your `INSTALLED_APPS`. If you want
216-
graphiQL, install `graphql_ws.django` app before `graphene_django` to serve a
217-
graphiql template that will work with websockets:
216+
graphiQL, install the `graphql_ws.django` app before `graphene_django` to serve
217+
a graphiQL template that will work with websockets:
218218

219219
```python
220220
INSTALLED_APPS = [
@@ -263,20 +263,14 @@ GRAPHENE = {
263263
```
264264

265265

266-
Finally, configure channels routing (it'll be served from `/subscriptions`):
266+
Finally, you can set up channels routing yourself (maybe using
267+
`graphql_ws.django.routing.websocket_urlpatterns` in your `URLRouter`), or you
268+
can just use one of the preset channels applications:
267269

268270
```python
269-
from channels.routing import ProtocolTypeRouter, URLRouter
270-
from graphql_ws.django.graphql_channels import (
271-
websocket_urlpatterns as graphql_urlpatterns
272-
)
273-
274-
application = ProtocolTypeRouter({"websocket": URLRouter(graphql_urlpatterns)})
275-
```
276-
277-
...and point to the application in Django settings
278-
```python
279-
ASGI_APPLICATION = 'yourproject.schema'
271+
ASGI_APPLICATION = 'graphql_ws.django.routing.application'
272+
# or
273+
ASGI_APPLICATION = 'graphql_ws.django.routing.auth_application'
280274
```
281275

282276
Run `./manage.py runserver` and go to `http://localhost:8000/graphql` to test!

README.rst

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,12 @@ Django Channels 2
215215
Set up with Django Channels just takes three steps:
216216

217217
1. Install the apps
218-
2. Set up schema
219-
3. Set up channels Router
218+
2. Set up your schema
219+
3. Configure the channels router application
220220

221221
First ``pip install channels`` and it to your ``INSTALLED_APPS``. If you
222-
want graphiQL, install ``graphql_ws.django`` app before
223-
``graphene_django`` to serve a graphiql template that will work with
222+
want graphiQL, install the ``graphql_ws.django`` app before
223+
``graphene_django`` to serve a graphiQL template that will work with
224224
websockets:
225225

226226
.. code:: python
@@ -269,23 +269,16 @@ Next, set up your graphql schema:
269269
'SCHEMA': 'yourproject.schema'
270270
}
271271
272-
Finally, configure channels routing (it'll be served from
273-
``/subscriptions``):
272+
Finally, you can set up channels routing yourself (maybe using
273+
``graphql_ws.django.routing.websocket_urlpatterns`` in your
274+
``URLRouter``), or you can just use one of the preset channels
275+
applications:
274276

275277
.. code:: python
276278
277-
from channels.routing import ProtocolTypeRouter, URLRouter
278-
from graphql_ws.django.graphql_channels import (
279-
websocket_urlpatterns as graphql_urlpatterns
280-
)
281-
282-
application = ProtocolTypeRouter({"websocket": URLRouter(graphql_urlpatterns)})
283-
284-
...and point to the application in Django settings
285-
286-
.. code:: python
287-
288-
ASGI_APPLICATION = 'yourproject.schema'
279+
ASGI_APPLICATION = 'graphql_ws.django.routing.application'
280+
# or
281+
ASGI_APPLICATION = 'graphql_ws.django.routing.auth_application'
289282
290283
Run ``./manage.py runserver`` and go to
291284
``http://localhost:8000/graphql`` to test!

examples/django_channels2/Pipfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ name = "pypi"
77

88
[packages]
99
graphql-ws = {path = "./../..", editable = true}
10-
channels = "*"
10+
channels = "==2.*"
1111
graphene-django = "*"
1212

1313
[requires]

examples/django_channels2/Pipfile.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/django_channels2/django_channels2/routing.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

examples/django_channels2/django_channels2/schema.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ def resolve_hello(self, info, **kwargs):
1010

1111

1212
class Subscription(graphene.ObjectType):
13-
1413
count_seconds = graphene.Int(up_to=graphene.Int())
1514

1615
def resolve_count_seconds(root, info, up_to=5):
Lines changed: 3 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,11 @@
11
"""
22
Django settings for django_channels2 project.
3-
4-
Generated by 'django-admin startproject' using Django 2.0.7.
5-
6-
For more information on this file, see
7-
https://docs.djangoproject.com/en/2.0/topics/settings/
8-
9-
For the full list of settings and their values, see
10-
https://docs.djangoproject.com/en/2.0/ref/settings/
113
"""
12-
13-
import os
14-
15-
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
16-
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
17-
18-
19-
# Quick-start development settings - unsuitable for production
20-
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/
21-
22-
# SECURITY WARNING: keep the secret key used in production secret!
234
SECRET_KEY = "0%1c709jhmggqhk&=tci06iy+%jedfxpcoai69jd8wjzm+k2f0"
24-
25-
# SECURITY WARNING: don't run with debug turned on in production!
265
DEBUG = True
276

28-
ALLOWED_HOSTS = []
297

30-
31-
# Application definition
32-
33-
INSTALLED_APPS = [
34-
"channels",
35-
"graphql_ws.django",
36-
"graphene_django",
37-
"django.contrib.admin",
38-
"django.contrib.auth",
39-
"django.contrib.contenttypes",
40-
"django.contrib.sessions",
41-
"django.contrib.messages",
42-
"django.contrib.staticfiles",
43-
]
44-
45-
MIDDLEWARE = [
46-
"django.middleware.security.SecurityMiddleware",
47-
"django.contrib.sessions.middleware.SessionMiddleware",
48-
"django.middleware.common.CommonMiddleware",
49-
"django.middleware.csrf.CsrfViewMiddleware",
50-
"django.contrib.auth.middleware.AuthenticationMiddleware",
51-
"django.contrib.messages.middleware.MessageMiddleware",
52-
"django.middleware.clickjacking.XFrameOptionsMiddleware",
53-
]
54-
55-
ROOT_URLCONF = "django_channels2.urls"
8+
INSTALLED_APPS = ["channels", "graphql_ws.django", "graphene_django"]
569

5710
TEMPLATES = [
5811
{
@@ -63,59 +16,14 @@
6316
"context_processors": [
6417
"django.template.context_processors.debug",
6518
"django.template.context_processors.request",
66-
"django.contrib.auth.context_processors.auth",
67-
"django.contrib.messages.context_processors.messages",
6819
]
6920
},
7021
}
7122
]
7223

73-
WSGI_APPLICATION = "django_channels2.wsgi.application"
74-
ASGI_APPLICATION = "django_channels2.routing.application"
75-
76-
77-
# Database
78-
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
7924

80-
DATABASES = {
81-
"default": {
82-
"ENGINE": "django.db.backends.sqlite3",
83-
"NAME": os.path.join(BASE_DIR, "db.sqlite3"),
84-
}
85-
}
86-
87-
88-
# Password validation
89-
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators
90-
91-
AUTH_PASSWORD_VALIDATORS = [
92-
{
93-
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator"
94-
},
95-
{"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator"},
96-
{"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator"},
97-
{"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator"},
98-
]
99-
100-
101-
# Internationalization
102-
# https://docs.djangoproject.com/en/2.0/topics/i18n/
103-
104-
LANGUAGE_CODE = "en-us"
105-
106-
TIME_ZONE = "UTC"
107-
108-
USE_I18N = True
109-
110-
USE_L10N = True
111-
112-
USE_TZ = True
113-
114-
115-
# Static files (CSS, JavaScript, Images)
116-
# https://docs.djangoproject.com/en/2.0/howto/static-files/
117-
118-
STATIC_URL = "/static/"
25+
ROOT_URLCONF = "django_channels2.urls"
26+
ASGI_APPLICATION = "graphql_ws.django.routing.application"
11927

12028

12129
GRAPHENE = {"MIDDLEWARE": [], "SCHEMA": "django_channels2.schema.schema"}
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
from django.conf.urls import url
2-
from django.contrib import admin
1+
from django.urls import path
32
from graphene_django.views import GraphQLView
43

5-
urlpatterns = [
6-
url(r"^admin/", admin.site.urls),
7-
url(r"^graphql/?$", GraphQLView.as_view(graphiql=True)),
8-
]
4+
urlpatterns = [path("graphql", GraphQLView.as_view(graphiql=True))]

examples/django_channels2/django_channels2/wsgi.py

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)