Skip to content

Commit a1d2ebc

Browse files
committed
Only send messages for operations that exist
1 parent 9bec86e commit a1d2ebc

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

graphql_ws/base.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ def on_close(self, connection_context):
125125
return connection_context.unsubscribe_all()
126126

127127
def send_message(self, connection_context, op_id=None, op_type=None, payload=None):
128-
message = self.build_message(op_id, op_type, payload)
129-
assert message, "You need to send at least one thing"
130-
return connection_context.send(message)
128+
if op_id is None or connection_context.has_operation(op_id):
129+
message = self.build_message(op_id, op_type, payload)
130+
return connection_context.send(message)
131131

132132
def build_message(self, id, op_type, payload):
133133
message = {}
@@ -137,6 +137,7 @@ def build_message(self, id, op_type, payload):
137137
message["type"] = op_type
138138
if payload is not None:
139139
message["payload"] = payload
140+
assert message, "You need to send at least one thing"
140141
return message
141142

142143
def send_execution_result(self, connection_context, op_id, execution_result):

graphql_ws/base_async.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ async def on_start(self, connection_context, op_id, params):
142142

143143
execution_result = self.execute(params)
144144

145+
connection_context.register_operation(op_id, execution_result)
145146
if is_awaitable(execution_result):
146147
execution_result = await execution_result
147148

@@ -157,7 +158,6 @@ async def on_start(self, connection_context, op_id, params):
157158
)
158159
except Exception as e:
159160
await self.send_error(connection_context, op_id, e)
160-
connection_context.remove_operation(op_id)
161161
else:
162162
try:
163163
await self.send_execution_result(
@@ -166,8 +166,16 @@ async def on_start(self, connection_context, op_id, params):
166166
except Exception as e:
167167
await self.send_error(connection_context, op_id, e)
168168
await self.send_message(connection_context, op_id, GQL_COMPLETE)
169+
connection_context.remove_operation(op_id)
169170
await self.on_operation_complete(connection_context, op_id)
170171

172+
async def send_message(
173+
self, connection_context, op_id=None, op_type=None, payload=None
174+
):
175+
if op_id is None or connection_context.has_operation(op_id):
176+
message = self.build_message(op_id, op_type, payload)
177+
return await connection_context.send(message)
178+
171179
async def on_operation_complete(self, connection_context, op_id):
172180
pass
173181

0 commit comments

Comments
 (0)