Aiohttp предоставляет менеджер контекста для создания сеанса клиента. Рекомендуется использовать один сеанс на множество http-запросов (в большинстве случаев для каждого приложения) https://aiohttp.readthedocs.io/en/stable/client_quickstart.html#make-a-request
Но графен использует распознаватели, которые необходимо объявить как метод класса:
http://docs.graphene-python.org/en/latest/execution/execute/ Для графена также существует исполнитель asyncio https://github.com/graphql-python/aiohttp-graphql Есть ли способ выполнить все преобразователи асинхронно с контекстом?
Пример:
async def get_task(session, api_url, id):
""" Function to resolve task from rest API"""
async with session.get(api_url+id) as response:
return await response.json()
class Query(graphene.ObjectType):
task = graphene.Field(Task)
async def resolve_task(self, info, session, id=1):
"""This method needs to be run
in async with aiohttp.ClientSession() as session:
context"""
return await get_task(session, url, id)
Я думаю о декораторе или промежуточном программном обеспечении с глобальной переменной, но это выглядит некрасиво. Есть ли больше недвижимости и питонического способа сделать это?