datadvance / DjangoChannelsGraphqlWs

Django Channels based WebSocket GraphQL server with Graphene-like subscriptions
MIT License
281 stars 83 forks source link

Exception inside application: No route found for path 'graphql'. #101

Open wlodevelopteam opened 1 year ago

wlodevelopteam commented 1 year ago

after handshake, we got this error. Exception inside application: No route found for path 'graphql'.

we implemented basic approach as below. whats wrong inside our approach?

### project/schema
class MySubscription(channels_graphql_ws.Subscription):
    notification_queue_limit = 64
    event = graphene.String()

    class Arguments:
        arg1 = graphene.String()
        arg2 = graphene.String()

    @staticmethod
    def subscribe(root, info, arg1, arg2):
        return ["group42"]

    @staticmethod
    def publish(payload, info, arg1, arg2):
        return MySubscription(event="Something has happened!")

class Subscription(graphene.ObjectType):
    my_subscription = MySubscription.Field()
### app/consumers.py
import channels_graphql_ws
from project.schema import schema

class MyGraphqlWsConsumer(channels_graphql_ws.GraphqlWsConsumer):
    schema = schema

    # send_keepalive_every = 42
    # strict_ordering = True

    async def on_connect(self, payload):
        print("New client connected!")
### project/asgi.py
import os
import channels
from django.urls import path
from django.core.asgi import get_asgi_application

from app.consumers import MyGraphqlWsConsumer

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'backend.settings')

django_asgi_app = get_asgi_application()

application = channels.routing.ProtocolTypeRouter({
    "http": get_asgi_application(),
    "websocket": channels.routing.URLRouter([
        path("/graphql", MyGraphqlWsConsumer),
    ])
})
djpritchett commented 1 year ago

I struggled with this for too long of a time. This is what worked for me. (notice no slashes in the graphql path)

websocket_urlpatterns = [
    django.urls.re_path(r"graphql", MyGraphqlWsConsumer.as_asgi()),
]

application = ProtocolTypeRouter({
    "http": django_asgi_app,
    "websocket": channels.auth.AuthMiddlewareStack(channels.routing.URLRouter(websocket_urlpatterns)),
})

hope it helps someone else.