mirror of
https://github.com/graphql-python/graphene.git
synced 2025-05-15 05:43:41 +03:00
40 lines
929 B
Python
40 lines
929 B
Python
"""
|
|
This file is used mainly as a bridge for thenable abstractions.
|
|
This includes:
|
|
- Promises
|
|
- Asyncio Coroutines
|
|
"""
|
|
|
|
try:
|
|
from promise import Promise, is_thenable
|
|
except ImportError:
|
|
|
|
def is_thenable(obj):
|
|
return False
|
|
|
|
|
|
try:
|
|
from inspect import isawaitable
|
|
from .thenables_asyncio import await_and_execute
|
|
except ImportError:
|
|
|
|
def isawaitable(obj): # type: ignore
|
|
return False
|
|
|
|
|
|
def maybe_thenable(obj, on_resolve):
|
|
"""
|
|
Execute a on_resolve function once the thenable is resolved,
|
|
returning the same type of object inputed.
|
|
If the object is not thenable, it should return on_resolve(obj)
|
|
"""
|
|
if isawaitable(obj):
|
|
return await_and_execute(obj, on_resolve)
|
|
|
|
if is_thenable(obj):
|
|
return Promise.resolve(obj).then(on_resolve)
|
|
|
|
# If it's not awaitable not a Promise, return
|
|
# the function executed over the object
|
|
return on_resolve(obj)
|