如何调试Django后端的GraphQL订阅?

问题描述:

我想将GraphQL订阅添加到后端GraphQL API.我可以使用内置GraphiQL的 graphene_django 调试订阅吗?

I'd like to add GraphQL subscriptions to a backend GraphQL API. Can I debug subscriptions with the graphene_django builtin GraphiQL?

# <django-project>/settings.py

...
from graphene_django.views import GraphQLView

urlpatterns = [
    ...,
    url(r'^graphql$', GraphQLView.as_view(graphiql=True)),
    ...,
]

根据

According to graphene-subscriptions/issues/1 it seems like it's possible to create a custom GraphQLCustomCoreBackend

class GraphQLCustomCoreBackend(GraphQLCoreBackend):
    def __init__(self, executor=None):
        # type: (Optional[Any]) -> None
        super().__init__(executor)
        self.execute_params['allow_subscriptions'] = True

并包含在

# <django-project>/urls.py

url_paths = [
    ...,
    path('graphql/', csrf_exempt(CustomGraphQLView.as_view(graphiql=True, backend=GraphQLCustomCoreBackend())), name='graphql'),
    ...,
]

覆盖默认值.但是尚未测试.

to override the default one. However not tested yet.