question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Django Channels Example issues

See original GitHub issue
  • Python version: 3.6
  • Operating System: OSX Sierra

Description

When testing the Django Channels examples, I went through some errors:

1: graphql_ws/django_channels.py file is not on the latest package version. But I downloaded it manually inside the package.

2: I was getting the error "Subscriptions are not allowed. You will need to either use the subscribe function or pass allow_subscriptions=True". And, since the GraphQLView object does not support the allow_subscriptions parameter, I changed it inside the package to True (just for testing).

3. After that, I was getting the error Subscription must return Async Iterable or Observable. Reading this issue, I decided to add 'MIDDLEWARE: [] into the GRAPHENE settings variable.

4. After that, I started receiving this error: 'AnonymousObservable' object has no attribute 'errors'. Then I got a little bit frustrated and stop trying 😅

Does anyone have a clue why this is happening?

Thanks!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:16 (3 by maintainers)

github_iconTop GitHub Comments

11reactions
ambientlightcommented, Apr 12, 2019

just discovered graphene and though of evaluating state of graphql integration with django. this might a bit late though:

For short:

  1. To resolve Subscriptions are not allowed. You will need to either use the subscribe function or pass allow_subscriptions=True after navigating the call stack I found we can pass a custom backend to GraphQLView that can custom exc_context parameters.
from graphql.backend import GraphQLCoreBackend
class GraphQLCustomCoreBackend(GraphQLCoreBackend):
    def __init__(self, executor=None):
        # type: (Optional[Any]) -> None
        super().__init__(executor)
        self.execute_params['allow_subscriptions'] = True
  1. Same way, used MIDDLEWARE: [] in GRAPHENE settings. as OP.
  2. To resolve ‘AnonymousObservable’ object has no attribute ‘errors’. a custom view can to be defined that would unbox execution_result from the Observable. (this is hacky) Using ExtraGraphQLView, AuthenticatedGraphQLView outlined in https://github.com/eamigo86/graphene-django-subscriptions/issues/2 didn’t work for me as they just as GraphQLView don’t handle observables returned from execute_graphql_request which is from graphql/execution/executor.py#L265
class GraphQLObservableUnboxingView(GraphQLView):
    def execute_graphql_request(
            self, request, data, query, variables, operation_name, show_graphiql=False
    ):
        target_result = None

        def override_target_result(value):
            nonlocal target_result
            target_result = value

        execution_result = super().execute_graphql_request(request, data, query, variables, operation_name, show_graphiql)
        if execution_result:
            if isinstance(execution_result, ObservableBase):
                target = execution_result.subscribe(on_next=lambda value: override_target_result(value))
                target.dispose()
            else:
                return execution_result

        return target_result

So in urls.py it can be:

url(r'^graphql', GraphQLObservableUnboxingView.as_view(graphiql=True, backend=GraphQLCustomCoreBackend()))

I have not used django channels and just tried reproducing the most possible minimal example so that subscription can be resolved:

subscription{
  subscribeToFoo(id: 1)
}
from rx import Observable
class Subscription(graphene.ObjectType):
    subscribe_to_foo = graphene.Boolean(id=graphene.Int())
    
    def resolve_subscribe_to_foo(self, args, **kwargs):
        return Observable.of(True)

schema = graphene.Schema(query=Query, subscription=Subscription)

also please make sure you are using rxpy 1.6.*

1reaction
MedNabilEssefaihicommented, Nov 26, 2021

Hello there,

I am trying to call a subscription from PostMan, but whenever run got is error { "errors": [ { "message": "Subscription must return Async Iterable or Observable. Received: <Promise at 0x2940d9c7cd0 rejected with AttributeError(\"'NoneType' object has no attribute 'register_subscription'\")>" } ], "data": null }

even when I did the same step as @ambientlight did. I can’t find any solution for that any help please!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues · django/channels
Is Django channels layer support AWS SQS? ... Consumer methods 'channel_layer.send', 'channel_layer.group_send' and 'channel_layer.receive' not ...
Read more >
Support — Channels 4.0.0 documentation
Make a fresh Django project (or use one of the Channels example projects) and make sure it doesn't have the bug, then work...
Read more >
Django 3 & Channels 3, a bad recipe. What can you do ...
We immediately started noticing an odd behavior where the server becomes unresponsive and unable to handle requests for a few seconds. The ...
Read more >
Introduction to Django Channels
In this tutorial, we'll build a real-time chat application with Django Channels, focusing on how to integrate Django with Django Channels.
Read more >
Django channels: Echo example issues
I'm following the sample code provided by the channels documentation and have run into a issue. The django server successfully accepts a ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found