Dead-simple to use GraphQL client over websocket. Using the apollo-transport-ws protocol.
pip install py-graphql-client
from graphql_client import GraphQLClient
ws = GraphQLClient('ws://localhost:8080/graphql')
def callback(_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 subscription
ws.stop_subscribe(sub_id)
ws.close()
from graphql_client import GraphQLClient
ws = GraphQLClient('ws://localhost:8080/graphql')
def callback(_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)
from graphql_client import GraphQLClient
ws = 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()
- currently wss doesn't not work. support wss.
- tests
- support http as well
- should use asyncio websocket library?