Skip to content

Commit e3e3ee7

Browse files
committed
Added request context for each server handle
1 parent 927ca27 commit e3e3ee7

File tree

4 files changed

+15
-22
lines changed

4 files changed

+15
-22
lines changed

graphql_ws/aiohttp.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ class AiohttpSubscriptionServer(BaseSubscriptionServer):
3939
def get_graphql_params(self, *args, **kwargs):
4040
params = super(AiohttpSubscriptionServer,
4141
self).get_graphql_params(*args, **kwargs)
42-
return dict(params, executor=AsyncioExecutor())
42+
return dict(params, return_promise=True, executor=AsyncioExecutor())
4343

44-
async def handle(self, ws):
45-
connection_context = AiohttpConnectionContext(ws)
44+
async def handle(self, ws, request_context=None):
45+
connection_context = AiohttpConnectionContext(ws, request_context)
4646
await self.on_open(connection_context)
4747
while True:
4848
try:
@@ -70,19 +70,13 @@ async def on_connection_init(self, connection_context, op_id, payload):
7070
try:
7171
await self.on_connect(connection_context, payload)
7272
await self.send_message(connection_context, op_type=GQL_CONNECTION_ACK)
73-
74-
# if self.keep_alive:
75-
# await self.send_message(connection_context,
76-
# op_type=GQL_CONNECTION_KEEP_ALIVE)
7773
except Exception as e:
7874
await self.send_error(connection_context, op_id, e, GQL_CONNECTION_ERROR)
7975
await connection_context.close(1011)
8076

81-
async def on_connection_terminate(self, connection_context, op_id):
82-
await connection_context.close(1011)
83-
8477
async def on_start(self, connection_context, op_id, params):
85-
execution_result = self.execute(return_promise=True, **params)
78+
execution_result = self.execute(
79+
connection_context.request_context, params)
8680

8781
if isawaitable(execution_result):
8882
execution_result = await execution_result

graphql_ws/base.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ class ConnectionClosedException(Exception):
2121

2222

2323
class BaseConnectionContext(object):
24-
def __init__(self, ws):
24+
def __init__(self, ws, request_context=None):
2525
self.ws = ws
2626
self.operations = {}
27+
self.request_context = request_context
2728

2829
def has_operation(self, op_id):
2930
return op_id in self.operations
@@ -167,13 +168,13 @@ def on_operation_complete(self, connection_context, op_id):
167168
pass
168169

169170
def on_connection_terminate(self, connection_context, op_id):
170-
return connection_context.close()
171+
return connection_context.close(1011)
171172

172-
def execute(self, **params):
173+
def execute(self, request_context, params):
173174
return graphql(
174175
self.schema, **dict(params, allow_subscriptions=True))
175176

176-
def handle(self, ws):
177+
def handle(self, ws, request_context=None):
177178
raise NotImplementedError("handle method not implemented")
178179

179180
def on_message(self, connection_context, message):

graphql_ws/gevent.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ def get_graphql_params(self, *args, **kwargs):
4242
self).get_graphql_params(*args, **kwargs)
4343
return dict(params, executor=SyncExecutor())
4444

45-
def handle(self, ws):
46-
connection_context = GeventConnectionContext(ws)
45+
def handle(self, ws, request_context=None):
46+
connection_context = GeventConnectionContext(ws, request_context)
4747
self.on_open(connection_context)
4848
while True:
4949
try:
@@ -75,12 +75,10 @@ def on_connection_init(self, connection_context, op_id, payload):
7575
self.send_error(connection_context, op_id, e, GQL_CONNECTION_ERROR)
7676
connection_context.close(1011)
7777

78-
def on_connection_terminate(self, connection_context, op_id):
79-
connection_context.close(1011)
80-
8178
def on_start(self, connection_context, op_id, params):
8279
try:
83-
execution_result = self.execute(**params)
80+
execution_result = self.execute(
81+
connection_context.request_context, params)
8482
assert isinstance(
8583
execution_result, Observable), "A subscription must return an observable"
8684
execution_result.subscribe(SubscriptionObserver(

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
long_description=readme + '\n\n' + history,
3737
author="Syrus Akbary",
3838
author_email='me@syrusakbary.com',
39-
url='https://github.com/graphql-python/graphql_ws',
39+
url='https://github.com/graphql-python/graphql-ws',
4040
packages=find_packages(include=['graphql_ws']),
4141
include_package_data=True,
4242
install_requires=requirements,

0 commit comments

Comments
 (0)