@@ -356,13 +356,11 @@ async def _subscribe(
356
356
# before a break if python version is too old (pypy3 py 3.6.1)
357
357
self ._generator = inner_generator
358
358
359
- async for result in inner_generator :
360
- if result .errors :
361
- # Note: we need to run generator.aclose() here or the finally block in
362
- # transport.subscribe will not be reached in pypy3 (py 3.6.1)
363
- await inner_generator .aclose ()
364
-
365
- yield result
359
+ try :
360
+ async for result in inner_generator :
361
+ yield result
362
+ finally :
363
+ await inner_generator .aclose ()
366
364
367
365
async def subscribe (
368
366
self , document : DocumentNode , * args , ** kwargs
@@ -372,17 +370,24 @@ async def subscribe(
372
370
373
371
The extra arguments are passed to the transport subscribe method."""
374
372
375
- # Validate and subscribe on the transport
376
- async for result in self ._subscribe (document , * args , ** kwargs ):
377
-
378
- # Raise an error if an error is returned in the ExecutionResult object
379
- if result .errors :
380
- raise TransportQueryError (
381
- str (result .errors [0 ]), errors = result .errors , data = result .data
382
- )
373
+ inner_generator : AsyncGenerator [ExecutionResult , None ] = self ._subscribe (
374
+ document , * args , ** kwargs
375
+ )
383
376
384
- elif result .data is not None :
385
- yield result .data
377
+ try :
378
+ # Validate and subscribe on the transport
379
+ async for result in inner_generator :
380
+
381
+ # Raise an error if an error is returned in the ExecutionResult object
382
+ if result .errors :
383
+ raise TransportQueryError (
384
+ str (result .errors [0 ]), errors = result .errors , data = result .data
385
+ )
386
+
387
+ elif result .data is not None :
388
+ yield result .data
389
+ finally :
390
+ await inner_generator .aclose ()
386
391
387
392
async def _execute (
388
393
self , document : DocumentNode , * args , ** kwargs
0 commit comments