Repository: strawberry-graphql/strawberry

Issue:
Subscriptions are returning data not wrapped into a field
It should return:

```json
{
  "data": {
    "time": 1
  }
}
```

![image](https://user-images.githubusercontent.com/667029/55296087-f77cc200-540c-11e9-9492-9aae63722765.png)

Repro:

```python
from __future__ import annotations


import asyncio
import strawberry

@strawberry.type
class Query:
    hello: str = "demo"

@strawberry.type
class Subscription:
    @strawberry.subscription
    async def time(self, info) -> int:
        i = 0
        while True:
            yield i
            i += 1

            await asyncio.sleep(1)


schema = strawberry.Schema(
    query=Query, subscription=Subscription
)
```



Discussion:

Resolving patch:
diff --git a/strawberry/contrib/starlette/app/graphql_ws_app.py b/strawberry/contrib/starlette/app/graphql_ws_app.py
--- a/strawberry/contrib/starlette/app/graphql_ws_app.py
+++ b/strawberry/contrib/starlette/app/graphql_ws_app.py
@@ -60,8 +60,10 @@ async def asgi(self, receive: Receive, send: Send, scope: Scope) -> None:
             operation_name=payload["operationName"],
         )
 
-        async for result in data.iterator:
-            await self._send_message(websocket, "data", {"data": result}, id_)
+        async for result in data:
+            # TODO: send errors if any
+
+            await self._send_message(websocket, "data", {"data": result.data}, id_)
 
         await self._send_message(websocket, "complete")
         await websocket.close()
diff --git a/strawberry/field.py b/strawberry/field.py
--- a/strawberry/field.py
+++ b/strawberry/field.py
@@ -56,7 +56,11 @@ def resolver(source, info, **args):
         return wrap(source, info, **args)
 
     if is_subscription:
-        kwargs = {"subscribe": resolver}
+
+        def _resolve(event, info):
+            return event
+
+        kwargs = {"subscribe": resolver, "resolve": _resolve}
     else:
         kwargs = {"resolve": resolver}
 
diff --git a/strawberry/type_converter.py b/strawberry/type_converter.py
--- a/strawberry/type_converter.py
+++ b/strawberry/type_converter.py
@@ -1,3 +1,5 @@
+from collections.abc import AsyncGenerator
+
 from graphql import (
     GraphQLBoolean,
     GraphQLFloat,
@@ -42,10 +44,17 @@ def get_graphql_type_for_annotation(
 
             return GraphQLList(list_of_type)
 
+        annotation_origin = getattr(annotation, "__origin__", None)
+
+        if annotation_origin == AsyncGenerator:
+            # async generators are used in subscription, we only need the yield type
+            # https://docs.python.org/3/library/typing.html#typing.AsyncGenerator
+            return get_graphql_type_for_annotation(annotation.__args__[0], field_name)
+
         # for some reason _name is None for Optional and Union types, so we check if we
         # have __args__ populated, there might be some edge cases where __args__ is
         # populated but the type is not an Union, like in the above case with Lists
-        if hasattr(annotation, "__args__"):
+        elif hasattr(annotation, "__args__"):
             types = annotation.__args__
             non_none_types = [x for x in types if x != None.__class__]  # noqa:E721
 


Test patch:
diff --git a/tests/test_subscription.py b/tests/test_subscription.py
--- a/tests/test_subscription.py
+++ b/tests/test_subscription.py
@@ -1,14 +1,19 @@
 import textwrap
+import typing
+
+import pytest
 
 import strawberry
+from graphql.language import parse
+from graphql.subscription import subscribe
 
 
-def test_simple_required_types():
+def test_subscription_type():
     @strawberry.type
     class MySub:
         @strawberry.subscription
-        async def x(self, info) -> str:
-            return "Hi"
+        async def x(self, info) -> typing.AsyncGenerator[str, None]:
+            yield "Hi"
 
     expected_representation = """
     type MySub {
@@ -17,3 +22,26 @@ async def x(self, info) -> str:
     """
 
     assert repr(MySub()) == textwrap.dedent(expected_representation).strip()
+
+
+@pytest.mark.asyncio
+async def test_subscription():
+    @strawberry.type
+    class Query:
+        x: str = "Hello"
+
+    @strawberry.type
+    class Subscription:
+        @strawberry.subscription
+        async def example(self, info) -> typing.AsyncGenerator[str, None]:
+            yield "Hi"
+
+    schema = strawberry.Schema(query=Query, subscription=Subscription)
+
+    query = "subscription { example }"
+
+    sub = await subscribe(schema, parse(query))
+    result = await sub.__anext__()
+
+    assert not result.errors
+    assert result.data["example"] == "Hi"