异步GraphQL帮助程序库
cannula的Python项目详细描述
套管
为什么要插管?GraphQL for people who like Python!
我们想让世界变得更美好,但我们是程序员,所以我们决定 再次让网络变得有趣。对javascript的关注太多了 客户端库。他们似乎都在规模、速度和功能上竞争,但是 他们中的大多数并不能解决你遇到的任何实际问题。所以当 todo应用程序快速易用,难的部分需要很长时间 完成。
现在如果你想要一个漂亮的单页应用程序,你需要投资 好的一周左右计划好所有的工具,你将需要组装你的网站。 当你搜索最新的趋势时,每一个决定都充满了悲伤和怀疑 或者如何设置单元测试。或者搜索 你喜欢的图书馆。
使用graphql可以简化web应用程序堆栈并减少 无遗憾地获得相同的客户体验。通过使用 只需要几个核心库,您就可以提高生产效率并 应用程序易于维护。
我们的理念:
- 使你的网站易于维护。
- 记录你的代码。
- 不要把自己锁在一个框架里。
- 开心点!
安装
需要Python3.6或更高版本!唯一的依赖是 graphql-core-next。
pip3 install cannula
快速启动
这是一个小的hello world example:
importloggingimporttypingimportsysimportcannulafromcannula.middlewareimportDebugMiddlewareSCHEMA=cannula.gql(""" type Message { text: String } type Query { hello(who: String): Message }""")logging.basicConfig(level=logging.DEBUG)api=cannula.API(__name__,schema=SCHEMA,middleware=[DebugMiddleware()])classMessage(typing.NamedTuple):text:str# The query resolver takes a source and info objects# and any arguments defined by the schema. Here we# only accept a single argument `who`.@api.resolver('Query')asyncdefhello(source,info,who):returnMessage(f"Hello, {who}!")# Pre-parse your query to speed up your requests.# Here is an example of how to pass arguments to your# query functions.SAMPLE_QUERY=cannula.gql(""" query HelloWorld ($who: String!) { hello(who: $who) { text } }""")who='world'iflen(sys.argv)>1:who=sys.argv[1]print(api.call_sync(SAMPLE_QUERY,variables={'who':who}))
现在,如果在命令行上运行示例,您将看到结果:
$ python3 examples/hello.py DEBUG:asyncio:Using selector: KqueueSelector DEBUG:cannula.schema:Adding default empty Mutation type DEBUG:cannula.middleware.debug:Resolving Query.hello expecting type Message DEBUG:cannula.middleware.debug:Field Query.hello resolved: Message(text='Hello, world!') in 0.000108 seconds DEBUG:cannula.middleware.debug:Resolving Message.text expecting type String DEBUG:cannula.middleware.debug:Field Message.text resolved: 'Hello, world!' in 0.000067 seconds ExecutionResult(data={'hello': {'text': 'Hello, world!'}}, errors=None ) $ python3 examples/hello.py Bob DEBUG:asyncio:Using selector: KqueueSelector DEBUG:cannula.schema:Adding default empty Mutation type DEBUG:cannula.middleware.debug:Resolving Query.hello expecting type Message DEBUG:cannula.middleware.debug:Field Query.hello resolved: Message(text='Hello, Bob!') in 0.000104 seconds DEBUG:cannula.middleware.debug:Resolving Message.text expecting type String DEBUG:cannula.middleware.debug:Field Message.text resolved: 'Hello, Bob!' in 0.000101 seconds ExecutionResult(data={'hello': {'text': 'Hello, Bob!'}}, errors=None )
但django integration或flask呢?
# pip install channels, Djangoimportcannulafromchannels.dbimportdatabase_sync_to_asyncfromdjango.contrib.auth.modelsimportUserschema=cannula.gql(""" type User { username: String # Only expose the fields you actually use first_name: String last_name: String made_up_field: String } extend type Query { getUserById(user_id: String): User }""")@api.query()asyncdefgetUserById(source,info,user_id):returnawaitget_user(user_id)@database_sync_to_asyncdefget_user(user_id):returnUser.objects.get(pk=user_id)@api.resolve('User')asyncdefmade_up_field(source,info):returnf"{source.get_full_name()} is a lying lier there is no 'made_up_field'"
因为graphql对您所需的数据存储在何处或如何存储是不可知的。 要做的是提供一个函数来解析查询。你返回的结果只是 需要匹配架构,您就完成了。
django和sqlalchemy已经提供了查询数据库的工具。而且他们 工作得很好。或者您可以选择使用异步数据库库 并发请求工作得更好。把它们都试一下,看看什么最适合 你的团队和用例。