Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize resolve routine #97

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions graphql_ws/base_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,28 @@ async def resolve(
Recursively wait on any awaitable children of a data element and resolve any
Promises.
"""
if is_awaitable(data):
data = await data
if isinstance(data, Promise):
data = data.value # type: Any
if _container is not None:
_container[_key] = data
if isinstance(data, dict):
items = data.items()
elif isinstance(data, list):
items = enumerate(data)
else:
items = None
if items is not None:
children = [
asyncio.ensure_future(resolve(child, _container=data, _key=key))
for key, child in items
]
if children:
await asyncio.wait(children)
stack = [(data, _container, _key)]

while stack:
data, _container, _key = stack.pop()

if is_awaitable(data):
data = await data
if isinstance(data, Promise):
data = data.value # type: Any
if _container is not None:
_container[_key] = data
if isinstance(data, dict):
items = data.items()
elif isinstance(data, list):
items = enumerate(data)
else:
items = None
if items is not None:
stack.extend([
(child, data, key)
for key, child in items
])


class BaseAsyncConnectionContext(base.BaseConnectionContext, ABC):
Expand Down