Skip to content

Commit 29408d7

Browse files
committed
Await subscribed execution results (#10)
1 parent 9d104eb commit 29408d7

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ a query language for APIs created by Facebook.
1212
[![Python 3 Status](https://pyup.io/repos/github/graphql-python/graphql-core-next/python-3-shield.svg)](https://pyup.io/repos/github/graphql-python/graphql-core-next/)
1313

1414
The current version 1.0.1 of GraphQL-core-next is up-to-date with GraphQL.js version
15-
14.0.2. All parts of the API are covered by an extensive test suite of currently 1618
15+
14.0.2. All parts of the API are covered by an extensive test suite of currently 1619
1616
unit tests.
1717

1818

@@ -182,7 +182,7 @@ Design goals for the GraphQL-core-next library are:
182182
* to be very close to the GraphQL.js reference implementation, while still using a
183183
Pythonic API and code style
184184
* making use of Python type hints, similar to how GraphQL.js makes use of Flow
185-
* using of [black](https://github.com/ambv/black) for automatic code formatting
185+
* using of [black](https://github.com/ambv/black) for automatic code formatting
186186
* replicate the complete Mocha-based test suite of GraphQL.js using
187187
[pytest](https://docs.pytest.org/)
188188

graphql/subscription/subscribe.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ async def map_source_to_response(payload):
7474
as it is nearly identical to the "ExecuteQuery" algorithm, for which `execute`
7575
is also used.
7676
"""
77-
return execute(
77+
result = execute(
7878
schema,
7979
document,
8080
payload,
@@ -83,6 +83,7 @@ async def map_source_to_response(payload):
8383
operation_name,
8484
field_resolver,
8585
)
86+
return await result if isawaitable(result) else result
8687

8788
return MapAsyncIterator(result_or_stream, map_source_to_response)
8889

tests/subscription/test_subscribe.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,3 +736,31 @@ def resolve_fn(event, _info):
736736

737737
with raises(StopAsyncIteration):
738738
await anext(subscription)
739+
740+
@mark.asyncio
741+
async def should_work_with_async_resolve_function():
742+
async def subscribe_fn(_event, _info):
743+
yield {"email": {"subject": "Hello"}}
744+
745+
async def resolve_fn(event, _info):
746+
return event
747+
748+
async_email_schema = email_schema_with_resolvers(subscribe_fn, resolve_fn)
749+
750+
subscription = await subscribe(
751+
async_email_schema,
752+
parse(
753+
"""
754+
subscription {
755+
importantEmail {
756+
email {
757+
subject
758+
}
759+
}
760+
}
761+
"""
762+
),
763+
)
764+
765+
payload = await anext(subscription)
766+
assert payload == ({"importantEmail": {"email": {"subject": "Hello"}}}, None)

0 commit comments

Comments
 (0)