|
| 1 | +import json |
| 2 | + |
| 3 | +from graphql import format_error, graphql |
| 4 | +from graphql.execution.executors.sync import SyncExecutor |
| 5 | +from rx import Observer, Observable |
| 6 | +from .server import BaseWebSocketSubscriptionServer, ConnectionContext, ConnectionClosedException |
| 7 | +from .constants import * |
| 8 | + |
| 9 | + |
| 10 | +class GEventConnectionContext(ConnectionContext): |
| 11 | + |
| 12 | + def receive(self): |
| 13 | + msg = self.ws.receive() |
| 14 | + return msg |
| 15 | + |
| 16 | + def send(self, data): |
| 17 | + if self.closed: |
| 18 | + return |
| 19 | + self.ws.send(data) |
| 20 | + |
| 21 | + @property |
| 22 | + def closed(self): |
| 23 | + return self.ws.closed |
| 24 | + |
| 25 | + def close(self, code): |
| 26 | + self.ws.close(code) |
| 27 | + |
| 28 | +class GeventSubscriptionServer(BaseWebSocketSubscriptionServer): |
| 29 | + |
| 30 | + def get_graphql_params(self, *args, **kwargs): |
| 31 | + params = super(GeventSubscriptionServer, self).get_graphql_params(*args, **kwargs) |
| 32 | + return dict(params, executor=SyncExecutor()) |
| 33 | + |
| 34 | + def handle(self, ws): |
| 35 | + connection_context = GEventConnectionContext(ws) |
| 36 | + self.on_open(connection_context) |
| 37 | + while True: |
| 38 | + try: |
| 39 | + if connection_context.closed: |
| 40 | + raise ConnectionClosedException() |
| 41 | + message = connection_context.receive() |
| 42 | + except ConnectionClosedException: |
| 43 | + self.on_close(connection_context) |
| 44 | + return |
| 45 | + self.on_message(connection_context, message) |
| 46 | + |
| 47 | + def on_message(self, connection_context, message): |
| 48 | + try: |
| 49 | + parsed_message = json.loads(message) |
| 50 | + assert isinstance( |
| 51 | + parsed_message, dict), "Payload must be an object." |
| 52 | + except Exception as e: |
| 53 | + self.send_error(connection_context, None, e) |
| 54 | + return |
| 55 | + |
| 56 | + self.process_message(connection_context, parsed_message) |
| 57 | + |
| 58 | + def on_open(self, connection_context): |
| 59 | + pass |
| 60 | + |
| 61 | + def on_connect(self, connection_context, payload): |
| 62 | + pass |
| 63 | + |
| 64 | + def on_close(self, connection_context): |
| 65 | + remove_operations = list(connection_context.operations.keys()) |
| 66 | + for op_id in remove_operations: |
| 67 | + self.unsubscribe(connection_context, op_id) |
| 68 | + |
| 69 | + def on_connection_init(self, connection_context, op_id, payload): |
| 70 | + try: |
| 71 | + self.on_connect(connection_context, payload) |
| 72 | + self.send_message(connection_context, op_type=GQL_CONNECTION_ACK) |
| 73 | + |
| 74 | + except Exception as e: |
| 75 | + self.send_error(connection_context, op_id, e, GQL_CONNECTION_ERROR) |
| 76 | + connection_context.close(1011) |
| 77 | + |
| 78 | + def on_connection_terminate(self, connection_context, op_id): |
| 79 | + connection_context.close(1011) |
| 80 | + |
| 81 | + |
| 82 | + def on_start(self, connection_context, op_id, params): |
| 83 | + try: |
| 84 | + execution_result = graphql( |
| 85 | + self.schema, **params, allow_subscriptions=True |
| 86 | + ) |
| 87 | + assert isinstance( |
| 88 | + execution_result, Observable), "A subscription must return an observable" |
| 89 | + execution_result.subscribe(SubscriptionObserver( |
| 90 | + connection_context, |
| 91 | + op_id, |
| 92 | + self.send_execution_result, |
| 93 | + self.send_error, |
| 94 | + self.on_close |
| 95 | + ) |
| 96 | + ) |
| 97 | + except Exception as e: |
| 98 | + self.send_error(connection_context, op_id, str(e)) |
| 99 | + |
| 100 | + def on_stop(self, connection_context, op_id): |
| 101 | + self.unsubscribe(connection_context, op_id) |
| 102 | + |
| 103 | + |
| 104 | +class SubscriptionObserver(Observer): |
| 105 | + |
| 106 | + def __init__(self, connection_context, op_id, send_execution_result, send_error, on_close): |
| 107 | + self.connection_context = connection_context |
| 108 | + self.op_id = op_id |
| 109 | + self.send_execution_result = send_execution_result |
| 110 | + self.send_error = send_error |
| 111 | + self.on_close = on_close |
| 112 | + |
| 113 | + def on_next(self, value): |
| 114 | + self.send_execution_result(self.connection_context, self.op_id, value) |
| 115 | + |
| 116 | + def on_completed(self): |
| 117 | + self.on_close(self.connection_context) |
| 118 | + |
| 119 | + def on_error(self, error): |
| 120 | + self.send_error(self.connection_context, self.op_id, error) |
0 commit comments