Skip to content

Django Channels Example issues #8

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
jonatasbaldin opened this issue Feb 7, 2018 · 16 comments
Closed

Django Channels Example issues #8

jonatasbaldin opened this issue Feb 7, 2018 · 16 comments

Comments

@jonatasbaldin
Copy link

  • 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!

@jonatasbaldin jonatasbaldin changed the title Django Channels issues Django Channels Example issues Feb 7, 2018
@nick-lehmann
Copy link

I have experienced exactly the same today, but after seeing your issue I have stopped trying already after the second step.

@jonatasbaldin
Copy link
Author

I'm whiling to help, but I don't understand too much about the Observables :(

@mlugowska
Copy link

I've checked that DjangoDebugMiddleware returns promise, so it cannot be set together with subscriptions and observables. But AnonymousObservable' object has no attribute 'errors still occures even when I added field errors to observable. I think that it's graphene bug (?). But I still really need to add subscriptions to my django app (graphene-django, python). Do you have any thoughts how can I do this? I know graphene-django-subscriptions lib, but does it really necessery to use serializer class only in case of subscriptions? Please, a little help will be desirable.

@colanconnon
Copy link
Contributor

@jonatasbaldin which version of channels are you using?

@fcalo
Copy link

fcalo commented Mar 16, 2018

Changing the urls.py it's solved
eamigo86/graphene-django-subscriptions#2

@xDHILEx
Copy link

xDHILEx commented Aug 11, 2018

I'm stuck on the AnonymousObserable issue as well. https://github.com/xDHILEx/graphqlwsdjangochannels. @colanconnon I'm using channels 1.1.8

@colanconnon
Copy link
Contributor

@xDHILEx I will have to look at this closer when I have some time

@adrianmoisey
Copy link

Has anyone managed to solve this? I'm getting the same issue.

@ambientlight
Copy link

ambientlight commented Apr 11, 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 'AnonymousObservable' object has no attribute 'errors' eamigo86/graphene-django-subscriptions#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.*

@hoffme
Copy link

hoffme commented Sep 20, 2019

what is it ObservableBase?

@carlosalvarez91
Copy link

carlosalvarez91 commented Dec 1, 2019

@ambientlight your solution seems to work when MIDDLEWARE: [ ], but since I'm using 'graphql_jwt.middleware.JSONWebTokenMiddleware', the rest of queries and mutations throws a Not logged in Error...

@ambientlight
Copy link

@phjocoronel2806: ObservableBase is from rxpy: https://github.com/ReactiveX/RxPY/blob/release/v1.6.x/rx/core/observablebase.py

@carlosalvarez91: haven't used any middleware with the above test so cannot yet give you any feedback on this.

@arturataide
Copy link

@carlosalvarez91 same issue as you.. Did you come up with any solution?

@carlosalvarez91
Copy link

@arturataide I ended up using this: https://github.com/datadvance/DjangoChannelsGraphqlWs

@SmileyChris
Copy link
Contributor

Django v2+ version now merged to master

@MedNabilEssefaihi
Copy link

MedNabilEssefaihi commented 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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests