1
1
import asyncio
2
+ from inspect import isawaitable
2
3
from typing import Any , AsyncGenerator , Dict , Generator , Optional , Union , cast
3
4
4
5
from graphql import (
@@ -87,16 +88,18 @@ def execute(self, document: DocumentNode, *args, **kwargs) -> Dict:
87
88
This function WILL BLOCK until the result is received from the server.
88
89
89
90
Either the transport is sync and we execute the query synchronously directly
90
- OR the transport is async and we execute the query in the asyncio loop (blocking here until answer)
91
+ OR the transport is async and we execute the query in the asyncio loop
92
+ (blocking here until answer).
91
93
"""
92
94
93
95
if isinstance (self .transport , AsyncTransport ):
94
96
95
97
loop = asyncio .get_event_loop ()
96
98
97
- assert (
98
- not loop .is_running ()
99
- ), "Cannot run client.execute if an asyncio loop is running. Use execute_async instead"
99
+ assert not loop .is_running (), (
100
+ "Cannot run client.execute if an asyncio loop is running."
101
+ " Use execute_async instead."
102
+ )
100
103
101
104
data : Dict [Any , Any ] = loop .run_until_complete (
102
105
self .execute_async (document , * args , ** kwargs )
@@ -109,9 +112,12 @@ def execute(self, document: DocumentNode, *args, **kwargs) -> Dict:
109
112
if self .schema :
110
113
self .validate (document )
111
114
112
- assert self .transport is not None , "Cant execute without a tranport"
115
+ assert self .transport is not None , "Cannot execute without a transport"
116
+
117
+ result = self .transport .execute (document , * args , ** kwargs )
113
118
114
- result : ExecutionResult = self .transport .execute (document , * args , ** kwargs )
119
+ assert not isawaitable (result ), "Transport returned an awaitable result."
120
+ result = cast (ExecutionResult , result )
115
121
116
122
if result .errors :
117
123
raise TransportQueryError (str (result .errors [0 ]))
@@ -146,9 +152,10 @@ def subscribe(
146
152
147
153
loop = asyncio .get_event_loop ()
148
154
149
- assert (
150
- not loop .is_running ()
151
- ), "Cannot run client.subscribe if an asyncio loop is running. Use subscribe_async instead"
155
+ assert not loop .is_running (), (
156
+ "Cannot run client.subscribe if an asyncio loop is running."
157
+ " Use subscribe_async instead."
158
+ )
152
159
153
160
try :
154
161
while True :
@@ -191,15 +198,20 @@ def __exit__(self, *args):
191
198
192
199
193
200
class ClientSession :
194
- """ An instance of this class is created when using 'async with' on the client.
201
+ """An instance of this class is created when using 'async with' on the client.
195
202
196
- It contains the async methods (execute, subscribe) to send queries with the async transports"""
203
+ It contains the async methods (execute, subscribe) to send queries
204
+ with the async transports.
205
+ """
197
206
198
207
def __init__ (self , client : Client ):
199
208
self .client = client
200
209
201
210
async def validate (self , document : DocumentNode ):
202
- """ Fetch schema from transport if needed and validate document if schema is present """
211
+ """Fetch schema from transport if needed and validate document.
212
+
213
+ If no schema is present, the validation will be skipped.
214
+ """
203
215
204
216
# Get schema from transport if needed
205
217
if self .client .fetch_schema_from_transport and not self .client .schema :
@@ -213,7 +225,7 @@ async def subscribe(
213
225
self , document : DocumentNode , * args , ** kwargs
214
226
) -> AsyncGenerator [Dict , None ]:
215
227
216
- # Fetch schema from transport if needed and validate document if schema is present
228
+ # Fetch schema from transport if needed and validate document if possible
217
229
await self .validate (document )
218
230
219
231
# Subscribe to the transport and yield data or raise error
@@ -224,7 +236,7 @@ async def subscribe(
224
236
async for result in self ._generator :
225
237
if result .errors :
226
238
# Note: we need to run generator.aclose() here or the finally block in
227
- # the transport.subscribe will not be reached in pypy3 (python version 3.6.1)
239
+ # transport.subscribe will not be reached in pypy3 (py 3.6.1)
228
240
await self ._generator .aclose ()
229
241
230
242
raise TransportQueryError (str (result .errors [0 ]))
@@ -234,7 +246,7 @@ async def subscribe(
234
246
235
247
async def execute (self , document : DocumentNode , * args , ** kwargs ) -> Dict :
236
248
237
- # Fetch schema from transport if needed and validate document if schema is present
249
+ # Fetch schema from transport if needed and validate document if possible
238
250
await self .validate (document )
239
251
240
252
# Execute the query with the transport with a timeout
0 commit comments