异步GraphQL帮助程序库

cannula的Python项目详细描述


套管

CircleCIDocumentation Status

GraphQL for people who like Python!

为什么要插管?

我们想让世界变得更美好,但我们是程序员,所以我们决定 再次让网络变得有趣。对javascript的关注太多了 客户端库。他们似乎都在规模、速度和功能上竞争,但是 他们中的大多数并不能解决你遇到的任何实际问题。所以当 todo应用程序快速易用,难的部分需要很长时间 完成。

现在如果你想要一个漂亮的单页应用程序,你需要投资 好的一周左右计划好所有的工具,你将需要组装你的网站。 当你搜索最新的趋势时,每一个决定都充满了悲伤和怀疑 或者如何设置单元测试。或者搜索 你喜欢的图书馆。

使用graphql可以简化web应用程序堆栈并减少 无遗憾地获得相同的客户体验。通过使用 只需要几个核心库,您就可以提高生产效率并 应用程序易于维护。

我们的理念:

  1. 使你的网站易于维护。
  2. 记录你的代码。
  3. 不要把自己锁在一个框架里。
  4. 开心点!

安装

需要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已经提供了查询数据库的工具。而且他们 工作得很好。或者您可以选择使用异步数据库库 并发请求工作得更好。把它们都试一下,看看什么最适合 你的团队和用例。

示例和文档

Documentation

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

推荐PyPI第三方库


热门话题
具有x86javapath的x64机器上x86java上的java JNI未满足链接错误   java将Pixmap的一部分上传到GPU   图像Java位图RLE8格式   java Android studio谷歌广告崩溃应用程序   java如何创建包含未知数量对象的变量?   Java计算给定int数组的所有可能组合   java JDBC classnotfound异常   httpclient中的java将HttpEntity转换为字符串的最优雅/正确的方法是什么?   如何从Java程序运行nano?   java在安卓中调用自定义类/方法   调用方法和JOptionPane后,允许代码继续执行所需的java计时器或其他想法   关于侦听器的向量Java并发问题   线程池执行器Java线程池   java配置DTO上的Swagger javax验证约束   Java中用于按钮功能的swing操作命令   ServletOutputStream中的java设置状态代码   java打印输入数组的平均值