Skip to content

Commit b6da149

Browse files
committed
Simplify django consumer now promises are resolved in the base
1 parent d879480 commit b6da149

1 file changed

Lines changed: 1 addition & 22 deletions

File tree

graphql_ws/django/consumers.py

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,11 @@
1-
import asyncio
21
import json
32

43
from channels.generic.websocket import AsyncJsonWebsocketConsumer
5-
from promise import Promise
64

75
from ..constants import WS_PROTOCOL
86
from .subscriptions import subscription_server
97

108

11-
class JSONPromiseEncoder(json.JSONEncoder):
12-
def encode(self, *args, **kwargs):
13-
self.pending_promises = []
14-
return super(JSONPromiseEncoder, self).encode(*args, **kwargs)
15-
16-
def default(self, o):
17-
if isinstance(o, Promise):
18-
if o.is_pending:
19-
self.pending_promises.append(o)
20-
return o.value
21-
return super(JSONPromiseEncoder, self).default(o)
22-
23-
249
class GraphQLSubscriptionConsumer(AsyncJsonWebsocketConsumer):
2510

2611
async def connect(self):
@@ -45,10 +30,4 @@ async def receive_json(self, content):
4530

4631
@classmethod
4732
async def encode_json(cls, content):
48-
json_promise_encoder = JSONPromiseEncoder()
49-
e = json_promise_encoder.encode(content)
50-
while json_promise_encoder.pending_promises:
51-
# Wait for pending promises to complete, then try encoding again.
52-
await asyncio.wait(json_promise_encoder.pending_promises)
53-
e = json_promise_encoder.encode(content)
54-
return e
33+
return json.dumps(content)

0 commit comments

Comments
 (0)