4747 inspect ,
4848 is_iterable ,
4949)
50- from ..pyutils .is_awaitable import is_awaitable as default_is_awaitable
50+ from ..pyutils .is_awaitable import (
51+ is_async_iterable as default_is_async_iterable ,
52+ )
53+ from ..pyutils .is_awaitable import (
54+ is_awaitable as default_is_awaitable ,
55+ )
5156from ..type import (
5257 GraphQLAbstractType ,
5358 GraphQLField ,
@@ -201,6 +206,9 @@ class ExecutionContext(IncrementalPublisherContext):
201206 is_awaitable : Callable [[Any ], TypeGuard [Awaitable ]] = staticmethod (
202207 default_is_awaitable # type: ignore
203208 )
209+ is_async_iterable : Callable [[Any ], TypeGuard [AsyncIterable ]] = staticmethod (
210+ default_is_async_iterable # type: ignore
211+ )
204212
205213 def __init__ (
206214 self ,
@@ -216,6 +224,7 @@ def __init__(
216224 enable_early_execution : bool = False ,
217225 middleware_manager : MiddlewareManager | None = None ,
218226 is_awaitable : Callable [[Any ], TypeGuard [Awaitable ]] | None = None ,
227+ is_async_iterable : Callable [[Any ], TypeGuard [AsyncIterable ]] | None = None ,
219228 ) -> None :
220229 self .schema = schema
221230 self .fragments = fragments
@@ -229,6 +238,7 @@ def __init__(
229238 self .enable_early_execution = enable_early_execution
230239 self .middleware_manager = middleware_manager
231240 self .is_awaitable = is_awaitable or default_is_awaitable
241+ self .is_async_iterable = is_async_iterable or default_is_async_iterable
232242 self .errors = None
233243 self .cancellable_streams = None
234244 self ._canceled_iterators : set [AsyncIterator ] = set ()
@@ -251,6 +261,7 @@ def build(
251261 enable_early_execution : bool = False ,
252262 middleware : Middleware | None = None ,
253263 is_awaitable : Callable [[Any ], TypeGuard [Awaitable ]] | None = None ,
264+ is_async_iterable : Callable [[Any ], TypeGuard [AsyncIterable ]] | None = None ,
254265 ** custom_args : Any ,
255266 ) -> list [GraphQLError ] | ExecutionContext :
256267 """Build an execution context
@@ -325,6 +336,7 @@ def build(
325336 enable_early_execution ,
326337 middleware_manager ,
327338 is_awaitable ,
339+ is_async_iterable ,
328340 ** custom_args ,
329341 )
330342
@@ -1041,7 +1053,7 @@ def complete_list_value(
10411053 """
10421054 item_type = return_type .of_type
10431055
1044- if isinstance (result , AsyncIterable ):
1056+ if self . is_async_iterable (result ):
10451057 async_iterator = result .__aiter__ ()
10461058
10471059 return self .complete_async_iterator_value (
@@ -1582,8 +1594,8 @@ def map_source_to_response(
15821594 as it is nearly identical to the "ExecuteQuery" algorithm,
15831595 for which :func:`~graphql.execution.execute` is also used.
15841596 """
1585- if not isinstance (result_or_stream , AsyncIterable ):
1586- return result_or_stream # pragma: no cover
1597+ if not self . is_async_iterable (result_or_stream ):
1598+ return cast ( "ExecutionResult" , result_or_stream ) # pragma: no cover
15871599
15881600 build_context = self .build_per_event_execution_context
15891601
@@ -2103,6 +2115,7 @@ def execute(
21032115 middleware : Middleware | None = None ,
21042116 execution_context_class : type [ExecutionContext ] | None = None ,
21052117 is_awaitable : Callable [[Any ], TypeGuard [Awaitable ]] | None = None ,
2118+ is_async_iterable : Callable [[Any ], TypeGuard [AsyncIterable ]] | None = None ,
21062119 ** custom_context_args : Any ,
21072120) -> AwaitableOrValue [ExecutionResult ]:
21082121 """Execute a GraphQL operation.
@@ -2137,6 +2150,7 @@ def execute(
21372150 middleware ,
21382151 execution_context_class ,
21392152 is_awaitable ,
2153+ is_async_iterable ,
21402154 ** custom_context_args ,
21412155 )
21422156 if isinstance (result , ExecutionResult ):
@@ -2167,6 +2181,7 @@ def experimental_execute_incrementally(
21672181 middleware : Middleware | None = None ,
21682182 execution_context_class : type [ExecutionContext ] | None = None ,
21692183 is_awaitable : Callable [[Any ], TypeGuard [Awaitable ]] | None = None ,
2184+ is_async_iterable : Callable [[Any ], TypeGuard [AsyncIterable ]] | None = None ,
21702185 ** custom_context_args : Any ,
21712186) -> AwaitableOrValue [ExecutionResult | ExperimentalIncrementalExecutionResults ]:
21722187 """Execute GraphQL operation incrementally (internal implementation).
@@ -2197,6 +2212,7 @@ def experimental_execute_incrementally(
21972212 enable_early_execution ,
21982213 middleware ,
21992214 is_awaitable ,
2215+ is_async_iterable ,
22002216 ** custom_context_args ,
22012217 )
22022218
@@ -2692,7 +2708,7 @@ def assert_event_stream(result: Any) -> AsyncIterable:
26922708 raise result
26932709
26942710 # Assert field returned an event stream, otherwise yield an error.
2695- if not isinstance (result , AsyncIterable ):
2711+ if not default_is_async_iterable (result ):
26962712 msg = (
26972713 "Subscription field must return AsyncIterable."
26982714 f" Received: { inspect (result )} ."
0 commit comments