Aiohttp提供了一个上下文管理器来创建客户端会话 . 建议每个http查询使用一个会话(在大多数情况下每个应用程序)https://aiohttp.readthedocs.io/en/stable/client_quickstart.html#make-a-request
但石墨烯使用旋转变压器需要声明为类方法:
http://docs.graphene-python.org/en/latest/execution/execute/对于石墨烯也存在asyncio Actuator 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)
我认为装饰器或中间件与全局变量,但它看起来很难看 . 是否有更多的庄园和pythonic方式来做到这一点?
1 回答
我会用
context
. 见https://docs.graphene-python.org/en/latest/execution/execute/例:
输出: