在aiohttp和graphql核心之上构建的asyncio graphql客户机next

aiographql-client的Python项目详细描述


Asyncio GraphQL客户端

接下来是在aiohttp和graphql核心之上构建的asyncio graphql客户机。默认情况下,客户机会在发送到服务器之前对模式进行内省并验证所有查询。

安装

pip install aiographql-client

示例用法

下面是这个客户机实现的一些示例用法。这些示例使用Hasura GraphQL Engine

简单查询

asyncdefget_bots():client=GraphQLClient(endpoint="http://localhost:8080/v1alpha1/graphql",headers={"x-hasura-admin-secret":"myadminsecretkey"},)request=GraphQLRequest(query="""        query get_bots {          chatbot {            id, bot_name          }        }        """)transaction=awaitclient.post(request)# display the query usedprint(transaction.request.query)# dump the response dataprint(transaction.response.data)

查询订阅

asyncdefget_bots():client=GraphQLClient(endpoint="http://localhost:8080/v1alpha1/graphql",headers={"x-hasura-admin-secret":"myadminsecretkey"},)request=GraphQLRequest(query="""        subscription get_bot_updates {          chatbot {            id, bot_name          }        }        """)# configure callbacks, here we simply print the event message when a data event# (`GraphQLSubscriptionEvent`) is received.callbacks=CallbackRegistry()callbacks.register(GraphQLSubscriptionEventType.DATA,lambdaevent:print(event.message))subscription:GraphQLSubscription=awaitclient.subscribe(request,callbacks)awaitsubscription.task

查询验证失败

如果您的查询无效,感谢graphql core next,我们将在回溯中得到一个详细的异常。

aiographql.client.exceptions.GraphQLClientValidationException: Query validation failed

Cannot query field 'ids' on type 'chatbot'. Did you mean 'id'?

GraphQL request (4:13)
3:           chatbot {
4:             ids, bot_names
               ^
5:           }

Cannot query field 'bot_names' on type 'chatbot'. Did you mean 'bot_name' or 'bot_language'?

GraphQL request (4:18)
3:           chatbot {
4:             ids, bot_names
                    ^
5:           }

示例请求

使用variablesoperationName

GraphQLRequest(query="""    query get_bot_created($id: Int) {      chatbot(where: {id: {_eq: $id}}) {        id, created      }    }    query get_bot_name($id: Int) {      chatbot(where: {id: {_eq: $id}}) {        id, bot_name      }    }    """,variables={'id':'109'},operationName='get_bot_name')

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

推荐PyPI第三方库


热门话题
java Android:在ListView上使用setOnItemClickListener   使用Netbeans 7.0连接到SQL Server的java正在挂起   java Spring3依赖项注入不适用于mule   java Flink SQL结果字段与LocalDateTime上请求的类型错误不匹配   java找不到文件的结尾   考虑到NamingStrategy,java有没有办法将字符串转换为JsonNode?   使用Netbeans/ant部署java(命令行)应用程序   java如何修复Spring引导多部分上载中的“所需请求部分不存在”   java在应用程序启动时通过引用获取映射未知目标实体属性异常   java形状旋转问题Java2d   Weblogic服务器上的java ExecuteAndWaitInterceptor问题   JavaSpringBoot:project将图像保存在错误的路径中,并且在使用IDEIntellji打开时不显示图像   类向java接口添加方法   Swing组件上的Java 7泛型   sql server如何从java获取用户名。sql。联系   java如何检查该行是否与正则表达式(regex)冲突?   java如何在spring引导安全中为计数失败登录设置验证登录为false   图像如何在Java中使PNG的白色透明?