Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial payload #93

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions channels_graphql_ws/graphql_ws_consumer.py
Original file line number Diff line number Diff line change
@@ -176,6 +176,8 @@ async def on_operation(self, op_id, payload):

# ------------------------------------------------------------------- IMPLEMENTATION

SKIP = object()

# A prefix of Channel groups with subscription notifications.
group_name_prefix: str = "GQLWS"

@@ -914,6 +916,10 @@ async def _on_gql_start__initialize_subscription_stream(
# `notification_queue_lock` as a guard while reading or writing
# to the queue.
notification_queue: asyncio.Queue = asyncio.Queue(maxsize=queue_size)
# Enqueue the initial payload.
initial_payload = subscription_class.initial_payload
if initial_payload is not self.SKIP:
notification_queue.put_nowait(Serializer.serialize(initial_payload))
# Lock to ensure that `notification_queue` operations are
# thread safe.
notification_queue_lock = threading.RLock()
5 changes: 5 additions & 0 deletions channels_graphql_ws/subscription.py
Original file line number Diff line number Diff line change
@@ -162,6 +162,11 @@ class Subscription(graphene.ObjectType):
# Useful to skip intermediate notifications, e.g. progress reports.
notification_queue_limit: Optional[int] = None

# Initial payload. Set it to send an "initial" response with this
# payload to a client as soon as it is subscribed (before any call
# to `Subscription.broadcast`).
initial_payload = GraphqlWsConsumer.SKIP

@classmethod
def broadcast(cls, *, group=None, payload=None):
"""Call this method to notify all subscriptions in the group.