Skip to content

Commit 2909d63

Browse files
author
Andrey Zelenchuk
committed
Introduce initial payload.
1 parent 09a2ffd commit 2909d63

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

channels_graphql_ws/graphql_ws_consumer.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ async def on_operation(self, op_id, payload):
176176

177177
# ------------------------------------------------------------------- IMPLEMENTATION
178178

179+
SKIP = object()
180+
179181
# A prefix of Channel groups with subscription notifications.
180182
group_name_prefix: str = "GQLWS"
181183

@@ -914,6 +916,10 @@ async def _on_gql_start__initialize_subscription_stream(
914916
# `notification_queue_lock` as a guard while reading or writing
915917
# to the queue.
916918
notification_queue: asyncio.Queue = asyncio.Queue(maxsize=queue_size)
919+
# Enqueue the initial payload.
920+
initial_payload = subscription_class.initial_payload
921+
if initial_payload is not self.SKIP:
922+
notification_queue.put_nowait(Serializer.serialize(initial_payload))
917923
# Lock to ensure that `notification_queue` operations are
918924
# thread safe.
919925
notification_queue_lock = threading.RLock()

channels_graphql_ws/subscription.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@ class Subscription(graphene.ObjectType):
162162
# Useful to skip intermediate notifications, e.g. progress reports.
163163
notification_queue_limit: Optional[int] = None
164164

165+
# Initial payload. Set it to send an "initial" response with this
166+
# payload to a client as soon as it is subscribed (before any call
167+
# to `Subscription.broadcast`).
168+
initial_payload = GraphqlWsConsumer.SKIP
169+
165170
@classmethod
166171
def broadcast(cls, *, group=None, payload=None):
167172
"""Call this method to notify all subscriptions in the group.

0 commit comments

Comments
 (0)