一个死的简单graphql客户端,支持通过websockets订阅。
py-graphql-client的Python项目详细描述
py graphql客户端
通过websocket使用graphql客户端非常简单。使用 apollo-transport-ws 协议。
示例
设置订阅超级容易
fromgraphql_clientimportGraphQLClientws=GraphQLClient('ws://localhost:8080/graphql')defcallback(_id,data):print("got new data..")print(f"msg id: {_id}. data: {data}")query=""" subscription { notifications { id title content } }"""sub_id=ws.subscribe(query,callback=callback)...# later stop the subscriptionws.stop_subscribe(sub_id)ws.close()
变量可以传递
fromgraphql_clientimportGraphQLClientws=GraphQLClient('ws://localhost:8080/graphql')defcallback(_id,data):print("got new data..")print(f"msg id: {_id}. data: {data}")query=""" subscription ($limit: Int!) { notifications (order_by: {created: "desc"}, limit: $limit) { id title content } }"""sub_id=ws.subscribe(query,variables={'limit':10},callback=callback)
正常的查询和突变也起作用
fromgraphql_clientimportGraphQLClientws=GraphQLClient('ws://localhost:8080/graphql')query=""" query ($limit: Int!) { notifications (order_by: {created: "desc"}, limit: $limit) { id title content } }"""res=ws.query(query,variables={'limit':10})print(res)ws.close()
待办事项
- 当前wss不工作。支持wss。
- 测试
- 也支持http
- 应该使用Asyncio WebSocket库吗?