asyncio+graphql=快速简单的api

aiographql的Python项目详细描述


用法

pip install aiographql

cat <<'END' >serve.py
import asyncio, aiographql, graphene

class User(graphene.ObjectType):
    id = graphene.ID(required=True)
    name = graphene.String()

class Query(graphene.ObjectType):
    me = graphene.Field(User)

    async def resolve_me(self, info):
        await asyncio.sleep(1)  # DB
        return User(id=42, name='John')

schema = graphene.Schema(query=Query, mutation=None)

aiographql.serve(schema, listen=[
    dict(protocol='tcp', port=25100),
    dict(protocol='unix', path='/tmp/worker0'),
])
END

python3 serve.py

curl http://localhost:25100/ --data-binary \
'{"query": "{
    me {
        id
        name
    }
}", "variables": null}'

# OR:
curl --unix-socket /tmp/worker0 http:/ --data-binary ...

# Result:
# 1 second async await for DB and then:
{"data":{"me":{"id":"42","name":"John"}}}

请参阅more examples and tests关于jwt身份验证、并发慢速数据库查询等。

config

import aiographql; help(aiographql.serve)

serve(schema, listen, get_context=None, exception_handler=None, enable_uvloop=True, run=True)
    Configure the stack and start serving requests
  • schemagraphene.Schema-要服务的GraphQL模式

  • listenlist-要侦听连接的一个或多个终结点:

  • get_contextNone[async] callable(loop, context: dict): mixed-从与exception_handler()统一的输入生成类似于图形的上下文身份验证

  • exception_handlerNonecallable(loop, context: dict)-在the docs+

    中定义的默认或自定义异常处理程序
    • ^{tt15}$: ^{tt16}$ or ^{tt9}$ - HTTP headers, if known
    • ^{tt18}$: ^{tt19}$ or ^{tt16}$ or ^{tt9}$ - accumulated HTTP request before content length is known, then accumulated content, then GraphQL request
  • enable_uvloopbool-启用uvloop以获得最佳性能,除非有更好的循环

  • runbool-如果True,则运行循环;False对于测试很好

  • 返回serversServers-await servers.close()以关闭侦听套接字-适合测试

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
字符串Java字母替换无效   java Spring Roo JPA MS SQL Server无法打开JPA EntityManager组织。冬眠例外GenericJDBCException:无法打开连接   在scala中使用JavaWS对大型数据文件进行java流式处理   Java编译器是否将字节和短字符识别为文本?   java无法查找符号错误,空指针   mongodb在Java中重用数据库连接   java将多个StringArray从字符串文件获取到活动中   java是一个变量,它只保存最后一次鼠标单击的坐标   c#尺寸有限;添加、删除和洗牌   java如何在Android中显示来自资产文件夹的文本文件中的文本   Android应用程序中的java Tensorflow Lite自定义对象检测模型错误   java如何在foreachloop中使用scanner将来自命令行的输入存储到数组中   java如何定义一个好的存储库接口   Android中的java解析动态json对象