graphql-ws icon indicating copy to clipboard operation
graphql-ws copied to clipboard

Pubsub implementation for easily publishing and subscribing from your resolvers

Open hballard opened this issue 7 years ago • 20 comments

@syrusakbary let me know your thoughts on this pull request when you get a chance. I modified the README and all the examples (except the Django one), to show how this would work.

hballard avatar Feb 25 '18 00:02 hballard

@hballard Wondering if python2.7 compatibility is needed?

kavink avatar Mar 06 '18 17:03 kavink

@kavink It should work fine now under python 2.7.

hballard avatar Mar 07 '18 03:03 hballard

@syrusakbary Any comments and feedback on if you would accept this in mainline ? cc: @hballard

Would be great if we get it in after whatever changes are needed.

kavink avatar Mar 08 '18 23:03 kavink

@hballard when I run Flask example and from graphql when I try to subscribe. I get following error

"Subscriptions are not allowed. You will need to either use the subscribe function or pass allow_subscriptions=True"

kavink avatar Mar 09 '18 02:03 kavink

@hballard instead of running under gunicorn, running from command line, it runs, But I dont see the results.

The subscription needs: A subscription must return an observable

But the type it gets is <class 'graphql.execution.base.ExecutionResult'>

So I am unable to subscribe , also Error is not showing up to end user, in Graphiql I see [object Object] mostly because its returning a json instead of execution result with error.

kavink avatar Mar 09 '18 04:03 kavink

I think I found the issue, I had to remove all other files except keep base, constants, gevent, pubsub/gevent_observable

kavink avatar Mar 09 '18 04:03 kavink

@kavink I need to see an example of your code to see where you are having an issue. The gevent example I modified runs fine on my machine, from the command line.

hballard avatar Mar 09 '18 04:03 hballard

@hballard Thanks, I got it working now from graphiql, Now im trying to make it run under foreman and gunicorn

kavink avatar Mar 09 '18 04:03 kavink

@hballard Awesome !!! thanks a lot for your work .. I Got it working https://bitbucket.org/noppo/gevent-websocket just had to provide worker-class. I am all set now . Thanks!

gunicorn -k "geventwebsocket.gunicorn.workers.GeventWebSocketWorker" wsgi:websocket_app

kavink avatar Mar 09 '18 05:03 kavink

Only annoyance right now is being required to have template.py with web socket url to make Graphiql work. And I cannot use GraphQLView.as_view

kavink avatar Mar 09 '18 05:03 kavink

@hballard @syrusakbary Now that I got Subscriptions working for simple example, wondering how can I do the following in RxPy, which will keep querying database using SQLAlchemy till a condition is met, I see there Observable.interval(1000) and Obervable.from_().

or maybe is it possible to plumb graphql-sqlalchemy with subscriptions so someone can be subscribed to a query ?

How can I create an Observable which will keep querying DB till condition is met and not query after condition is met, and just display last response ?

or maybe this a worth another issue for discussion in community on how to do it ?

kavink avatar Mar 10 '18 00:03 kavink

@kavink You can query the database for a condition, but you don't really need to. That is what you can use these classes for. Your mutations will usually be tied to updating a database. Just publish each mutation to a channel (inside your mutation resolver), after you write a mutation to your database and before it returns, and then any subscriptions subscribed to that channel (in their resolver) will automatically be updated. See this article for some good background on this concept. Probably move this to another issue (and not this pull request) if you need further discussion.

hballard avatar Mar 10 '18 01:03 hballard

@hballard The pubsub does not work if the definition of is a global space. I am trying to import it from a celery worker and publish it. if I define it in one file pub/sub works.Any thoughts on how can I manage the global state of subscriptions? Or maybe I shouldn't use celery and migrate to RxPy

kavink avatar Mar 13 '18 17:03 kavink

@hballard figured it out.. I should be using GeventRxRedisPubsub and NOT GeventRxPubsub so I can publish from celery worker and subscribe from flask/graphql in schema/subscriptions

kavink avatar Mar 16 '18 00:03 kavink

@hballard @syrusakbary Any chance we can know whether this is getting merged or not? I'm trying to setup subscriptions and this seems like it changes things around a fair amount

benwis avatar Jun 14 '18 16:06 benwis

@hballard nice work on this; thank you! Was able to get the aio version working without much issue at all and am also hoping this can be merged soon.

The only thing I encountered was packaging-related; I think you'll want to replace the include kwarg on the find_packages line in setup.py to "everything but tests," find_packages(exclude=["*.tests", "*.tests.*", "tests.*", "tests"]) (as described in the setuptools docs), because otherwise setuptools doesn't find your new graphql_ws.pubsub package.

bendemaree avatar Jun 14 '18 21:06 bendemaree

Thanks @bendemaree! @benwis I don't know that this "changes things around" much with the base library...you can still use the base library as is, without the classes contained in this PR. However, the PR adds simple classes that utilize a pubsub / observer pattern that you would probably need to implement yourself to get true working "subscriptions". This is a similar mechanism to what the ApolloGraphql JS subscriptions library does. @syrusakbary let me know if you'd like me to make any modifications to this PR to get it approved.

hballard avatar Jun 15 '18 03:06 hballard

@hballard Any chance you can tell me whether this or your graphql-python-subscriptions is the better package to use? Your documentation looks quite a bit nicer but it doesn't look like it's been updated in a while. Thanks!

benwis avatar Jun 15 '18 20:06 benwis

I'd use this repo, as opposed to the other one I wrote. I've pretty much deprecated it in favor of this repo once @syrusakbary released it, since it somewhat more official and a part of the graphql-python repo.

hballard avatar Jun 16 '18 04:06 hballard

@kavink How did you solve the Subscriptions are not allowed problem?

nayak16 avatar Aug 09 '18 22:08 nayak16