Hi,
I'm trying to use subscriptions in graphene 3.
My naive example below doesn't work. How do I do this?
Thanks,
Rob
import asyncio
from datetime import datetime
from graphene import ObjectType, String, Schema, Field
class Query(ObjectType):
hello = String()
def resolve_hello(root, info):
return 'Hello, world!'
class Subscription(ObjectType):
time_of_day = Field(String)
async def resolve_time_of_day(root, info):
while True:
yield datetime.now().isoformat()
await asyncio.sleep(1)
schema = Schema(query=Query, subscription=Subscription)
async def main():
subscription = 'subscription { timeOfDay }'
result = await schema.execute_async(subscription)
async for item in result:
print(item)
asyncio.run(main())
I've had a go at adding support for this.
See: #1107
This is what I ended up doing:
https://github.com/fellowinsights/graphene-asgi/blob/master/graphene_asgi/application.py#L18
Hi @ericls ,
That looks similar to what I did :) It would be good to get this in the main code and not have to patch it.
Rob
I tried to run above example. The same seems to be in official documentation also.
I got following error:
Traceback (most recent call last):
File "subscribe.py", line 29, in <module>
asyncio.run(main(SCHEMA))
File "C:\Users\m00383290\AppData\Local\Programs\Python\Python38-32\lib\asyncio\runners.py", line 43, in run
return loop.run_until_complete(main)
File "C:\Users\m00383290\AppData\Local\Programs\Python\Python38-32\lib\asyncio\base_events.py", line 616, in run_until_complete
return future.result()
File "subscribe.py", line 25, in main
result = await schema.subscribe(subscription)
File "D:\workspace\dev\kns\lib\site-packages\graphene\types\schema.py", line 98, in __getattr__
raise AttributeError('Type "{}" not found in the Schema'.format(type_name))
AttributeError: Type "subscribe" not found in the Schema
Am I doing something wrong, or is this issue not being resolved?
Thanks
I don't think this has been released yet. It only got merged into master yesterday.
I'm guessing the docs get automatically updated from master, but the pypi package has yet to be released.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
not stale?
Not stale. This is done :)
I'l close this issue
@rob-blackbourn could you provide a guide on how to implement subscription on v3? do I need to install django-channels? or? (for context, this is for django deployment)
would love to get some guidance :) thanks!
Hi @hyusetiawan
There's been some major changes here, so it will take a little time. However I'm very keen to see where we are, and how things work.
I'll try and get a demo working sometime this week.
Rob
@rob-blackbourn appreciate it mate, I shall be anxiously waiting while I try to experiment on my end!
@rob-blackbourn eagerly waiting for your demo. :) Thanks Dude
Hi @hyusetiawan and @SenthilKumaranC
I've made a repo here with a working example of graphene 3 with
queries, mutations, and subscriptions, using twitter as the data source.
I will write a tutorial of this fairly soon, but this may be useful to you as it is.
BTW ti uses a fork of graphene3 which includes a fix that hasn't yet been included in a published package.
I'd appreciate any feedback you have.
Rob
I written this up: https://medium.com/@rob.blackbourn/graphene-3-queries-mutations-subscriptions-a699926ca469
@rob-blackbourn Thanks for the example setup, would you find a moment to show how this could work alongside Django Channels?
Hi @WTK
Unfortunately I've never used Django. I had a quick look at the packages referenced here to see If I could knock up a quick example. I don't think any of them support graphene3 yet, but I could be wrong.
Ok, thanks for checking.
Regarding graphene 3 implementation, is it fair to say it's not concerned with a problem of maintaining subscribers state when dealing with multi-server setup? AFAIK there's gotta be a layer using which servers can learn about messages to send back to the subscribers connected to any of the servers, even if the message originates in just one of them. Correct?
@WTK
Not sure I understand yet. I think an example will help.
Lets say we have a chat application using graphene. You can send messages to the group via a mutation, and subscribe to messages sent from others. The subscription would a persistent connection to one of the servers (typically a WebSocket).
In the case of a multi-server setup there would have to be some kind of communication between each server. When a user sent a message to a server, that server would have to tell the other servers, so they could send messages to their subscribers.
I this scenario you might use Redis (using pub/sub), RabbitMQ, or a MongoDB cluster (using watch).
Is this what you mean?
Yes, that's exactly that. There must be a pub/sub layer. I suspect that this layer would have to be handled by graphene, right? I mean graphene must be aware of it to work fully?
Interesting!
I think this is an architectural question that you could argue from both sides.
In my view I don't think graphene is part of this picture. Graphene is all about parsing queries, calling "resolvers" and producing responses. It knows nothing about networks or communication mechanisms. In this view it has the single responsibility to process queries.
Looking at our chat example I think the chat "service" would be responsible for keeping things in sync. In a way my twitter example demonstrates this. A user can update their status (by sending a mutation) to one server, and other users will be informed to the update (through their subscription) regardless of the server they are connected to. The char service (twitter) handles the synchronisation.
Having said that, synchronisation will be a common problem that many graphene apps will have to solve. While I don't see this as the responsibility of graphene it feels a bit like aiodataloader; while it's not part of grapahen, it's part of the eco-system.
What do you think?
Most helpful comment
I written this up: https://medium.com/@rob.blackbourn/graphene-3-queries-mutations-subscriptions-a699926ca469