graphql-ws
graphql-ws copied to clipboard
Pubsub implementation for easily publishing and subscribing from your resolvers
@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 Wondering if python2.7 compatibility is needed?
@kavink It should work fine now under python 2.7.
@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.
@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"
@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.
I think I found the issue, I had to remove all other files except keep base, constants, gevent, pubsub/gevent_observable
@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 Thanks, I got it working now from graphiql, Now im trying to make it run under foreman and gunicorn
@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
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
@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 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 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
@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
@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
@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.
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 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!
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.
@kavink How did you solve the Subscriptions are not allowed
problem?