You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
By adding print statements, I was able to deduce, that the async for event in subscriber:-loop is never entered though broadcast.publish(...) is executed. This is only the case when run with Daphne. When run with Uvicorn, the loop is entered as expected and the result for the subscription is returned.
Any help would be greatly appreciated.
Here is the code:
import json
from ariadne import MutationType, SubscriptionType, make_executable_schema
from ariadne.asgi import GraphQL
from ariadne.asgi.handlers import GraphQLWSHandler
from broadcaster import Broadcast
from starlette.applications import Starlette
broadcast = Broadcast("memory://")
type_defs = """
type Query {
_unused: Boolean
}
type Message {
sender: String
message: String
}
type Mutation {
send(sender: String!, message: String!): Boolean
}
type Subscription {
message: Message
}
"""
mutation = MutationType()
@mutation.field("send")
async def resolve_send(*_, **message):
await broadcast.publish(channel="chatroom", message=json.dumps(message))
return True
subscription = SubscriptionType()
@subscription.source("message")
async def source_message(_, info):
async with broadcast.subscribe(channel="chatroom") as subscriber:
async for event in subscriber:
# >>>> This is never executed when run with daphne:
yield json.loads(event.message)
# <<<<
schema = make_executable_schema(type_defs, mutation, subscription)
graphql = GraphQL(
schema=schema,
debug=True,
websocket_handler=GraphQLWSHandler(),
)
app = Starlette(
debug=True,
on_startup=[broadcast.connect],
on_shutdown=[broadcast.disconnect],
)
app.mount("/", graphql)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I am having problems getting broadcaster (with Memory-Backend) to work with Daphne. Specifically, I would like to get this simple example to work:
Ariadne GraphQL Subscription Example (code below)
The app works as intended when run with Uvicorn but not in Daphne.
By adding print statements, I was able to deduce, that the
async for event in subscriber
:-loop is never entered thoughbroadcast.publish(...)
is executed. This is only the case when run with Daphne. When run with Uvicorn, the loop is entered as expected and the result for the subscription is returned.Any help would be greatly appreciated.
Here is the code:
Beta Was this translation helpful? Give feedback.
All reactions