Giter Club home page Giter Club logo

Comments (16)

ambientlight avatar ambientlight commented on July 26, 2024 11

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 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.*

from graphql-ws.

MedNabilEssefaihi avatar MedNabilEssefaihi commented on July 26, 2024 2

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!

from graphql-ws.

carlosalvarez91 avatar carlosalvarez91 commented on July 26, 2024 1

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

from graphql-ws.

nick-lehmann avatar nick-lehmann commented on July 26, 2024

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

from graphql-ws.

jonatasbaldin avatar jonatasbaldin commented on July 26, 2024

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

from graphql-ws.

mlugowska avatar mlugowska commented on July 26, 2024

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.

from graphql-ws.

colanconnon avatar colanconnon commented on July 26, 2024

@jonatasbaldin which version of channels are you using?

from graphql-ws.

fcalo avatar fcalo commented on July 26, 2024

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

from graphql-ws.

xDHILEx avatar xDHILEx commented on July 26, 2024

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

from graphql-ws.

colanconnon avatar colanconnon commented on July 26, 2024

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

from graphql-ws.

adrianmoisey avatar adrianmoisey commented on July 26, 2024

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

from graphql-ws.

hoffme avatar hoffme commented on July 26, 2024

what is it ObservableBase?

from graphql-ws.

carlosalvarez91 avatar carlosalvarez91 commented on July 26, 2024

@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...

from graphql-ws.

ambientlight avatar ambientlight commented on July 26, 2024

@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.

from graphql-ws.

arturataide avatar arturataide commented on July 26, 2024

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

from graphql-ws.

SmileyChris avatar SmileyChris commented on July 26, 2024

Django v2+ version now merged to master

from graphql-ws.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.