Skip to content

started on channels 2 updates (WIP) #9

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 6 additions & 4 deletions examples/django_subscriptions/django_subscriptions/asgi.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
from channels.asgi import get_channel_layer

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_subscriptions.settings")
import os
import django
from channels.routing import get_default_application

channel_layer = get_channel_layer()
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "multichat.settings")
django.setup()
application = get_default_application()
18 changes: 18 additions & 0 deletions examples/django_subscriptions/django_subscriptions/routing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from graphql_ws.django_channels import GraphQLSubscriptionConsumer
from django.urls import path

from channels.http import AsgiHandler
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
import logging

logger = logging.getLogger('django')


application = ProtocolTypeRouter({
"websocket": AuthMiddlewareStack(
URLRouter([
path("subscriptions", GraphQLSubscriptionConsumer),
])
),
})
24 changes: 14 additions & 10 deletions examples/django_subscriptions/django_subscriptions/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

ALLOWED_HOSTS = []

ASGI_APPLICATION = "django_subscriptions.routing.application"

# Application definition

Expand All @@ -40,6 +41,7 @@
'channels',
]


MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
Expand Down Expand Up @@ -119,19 +121,21 @@
# https://docs.djangoproject.com/en/1.11/howto/static-files/

STATIC_URL = '/static/'
CHANNELS_WS_PROTOCOLS = ["graphql-ws", ]
CHANNEL_LAYERS = {
"default": {
"BACKEND": "asgi_redis.RedisChannelLayer",
"CONFIG": {
"hosts": [("localhost", 6379)],
},
"ROUTING": "django_subscriptions.urls.channel_routing",
},
CHANNELS_WS_PROTOCOLS = ["graphql-ws"]


}


GRAPHENE = {
'SCHEMA': 'django_subscriptions.schema.schema'
}

LOGGING = {
'version': 1,
'handlers': {
'console': {
'level': 'INFO',
'class': 'logging.StreamHandler',
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def render_graphiql():
</html>''').substitute(
GRAPHIQL_VERSION='0.11.10',
SUBSCRIPTIONS_TRANSPORT_VERSION='0.7.0',
subscriptionsEndpoint='ws://localhost:8000/subscriptions',
subscriptionsEndpoint='ws://127.0.0.1:8000/subscriptions',
# subscriptionsEndpoint='ws://localhost:5000/',
endpointURL='/graphql',
)
5 changes: 0 additions & 5 deletions examples/django_subscriptions/django_subscriptions/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,4 @@ def graphiql(request):
url(r'^graphql', csrf_exempt(GraphQLView.as_view(graphiql=True)))
]

from channels.routing import route_class
from graphql_ws.django_channels import GraphQLSubscriptionConsumer

channel_routing = [
route_class(GraphQLSubscriptionConsumer, path=r"^/subscriptions"),
]
1 change: 0 additions & 1 deletion graphql_ws/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ def process_message(self, connection_context, parsed_message):
op_id = parsed_message.get('id')
op_type = parsed_message.get('type')
payload = parsed_message.get('payload')

if op_type == GQL_CONNECTION_INIT:
return self.on_connection_init(connection_context, op_id, payload)

Expand Down
46 changes: 21 additions & 25 deletions graphql_ws/django_channels.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from channels.generic.websockets import JsonWebsocketConsumer
from channels.generic.websocket import JsonWebsocketConsumer
from .base import BaseConnectionContext
import json
from graphql.execution.executors.sync import SyncExecutor
Expand All @@ -18,26 +18,28 @@

class DjangoChannelConnectionContext(BaseConnectionContext):

def __init__(self, message, request_context = None):
self.message = message
def __init__(self, send_json, close, request_context):
self.send_json = send_json
self.close = close
self.operations = {}
self.request_context = request_context

def send(self, data):
self.message.reply_channel.send(data)
self.send_json(data)

def close(self, reason):
data = {
'close': True,
'text': reason
}
self.message.reply_channel.send(data)
self.close()

class DjangoChannelSubscriptionServer(BaseSubscriptionServer):

def get_graphql_params(self, *args, **kwargs):
params = super(DjangoChannelSubscriptionServer,
self).get_graphql_params(*args, **kwargs)
def get_graphql_params(self, connection_context, payload):
params = {
'request_string': payload.get('query'),
'variable_values': payload.get('variables'),
'operation_name': payload.get('operationName'),
'context_value': connection_context.request_context,
}

return dict(params, executor=SyncExecutor())

def handle(self, message, connection_context):
Expand All @@ -53,7 +55,7 @@ def send_message(self, connection_context, op_id=None, op_type=None, payload=Non
message['payload'] = payload

assert message, "You need to send at least one thing"
return connection_context.send({'text': json.dumps(message)})
return connection_context.send(message)

def on_open(self, connection_context):
pass
Expand Down Expand Up @@ -96,23 +98,17 @@ def on_stop(self, connection_context, op_id):


class GraphQLSubscriptionConsumer(JsonWebsocketConsumer):
http_user_and_session = True
strict_ordering = True

def connect(self, message, **kwargs):
message.reply_channel.send({"accept": True})


def receive(self, content, **kwargs):
"""
Called when a message is received with either text or bytes
filled out.
"""
self.connection_context = DjangoChannelConnectionContext(self.message)
def connect(self):
self.accept()
self.connection_context = DjangoChannelConnectionContext(self.send_json, self.close, self.scope)
self.subscription_server = DjangoChannelSubscriptionServer(graphene_settings.SCHEMA)
self.subscription_server.on_open(self.connection_context)

def receive_json(self, content):
self.subscription_server.handle(content, self.connection_context)


class SubscriptionObserver(Observer):

def __init__(self, connection_context, op_id, send_execution_result, send_error, on_close):
Expand Down