async def subscribe( self, document: DocumentNode, *args, **kwargs, ) -> AsyncGenerator[ExecutionResult, None]: """Send a query and receive the results using an async generator The query can be a graphql query, mutation or subscription The results are sent as an ExecutionResult object """ subscribe_result = subscribe(self.schema, document, *args, **kwargs) if isinstance(subscribe_result, ExecutionResult): yield ExecutionResult else: # if we don't get an ExecutionResult, then we should receive # a Coroutine returning an AsyncIterator[ExecutionResult] subscribe_coro = cast( Coroutine[Any, Any, AsyncIterator[ExecutionResult]], subscribe_result) subscribe_generator = await subscribe_coro async for result in subscribe_generator: yield result
def test_accepts_an_object_with_named_properties_as_arguments(): document = parse(''' subscription { importantEmail } ''') result = subscribe(email_schema, document, root_value=None) assert isinstance(result, Observable)
def test_accepts_an_object_with_named_properties_as_arguments(): document = parse(''' subscription { importantEmail } ''') result = subscribe( email_schema, document, root_value=None ) assert isinstance(result, Observable)
def execute(self, *args, **kwargs): operation_ast = get_operation_ast(args[0]) if operation_ast and operation_ast.operation == "subscription": result = subscribe(self.schema, *args, **kwargs) if isinstance(result, Observable): a = [] result.subscribe(lambda x: a.append(x)) if len(a) > 0: result = a[-1] return result return execute(self.schema, *args, **kwargs)
def test_returns_an_error_if_subscribe_function_returns_error(): exc = Exception("Throw!") def thrower(root, info): raise exc erroring_email_schema = email_schema_with_resolvers(thrower) result = subscribe(erroring_email_schema, parse(''' subscription { importantEmail } ''')) assert result.errors == [exc]
def test_returns_an_error_if_subscribe_function_returns_error(): exc = Exception("Throw!") def thrower(root, info): raise exc erroring_email_schema = email_schema_with_resolvers(thrower) result = subscribe( erroring_email_schema, parse(''' subscription { importantEmail } ''')) assert result.errors == [exc]
def test_returns_an_error_if_subscribe_function_returns_error(): # type: () -> None exc = Exception("Throw!") def thrower(root, info): # type: (Optional[Any], ResolveInfo) -> None raise exc erroring_email_schema = email_schema_with_resolvers(thrower) result = subscribe( erroring_email_schema, parse(""" subscription { importantEmail } """), ) assert result.errors == [exc]