A simple GraphQL client that works over Websocket as the transport protocol, instead of HTTP.
Project description
Ob-Graphql
Setup a simple GraphQL client over websocket using apollo-transport-ws protocol.
GraphQL over WebSocket Protocol
Client-server communication
Each message has a type
field, which defined in the protocol of this package, as well as associated fields inside payload
field, depending on the message type, and id
field so the client can identify each response from the server.
Each WebSocket message is represented in JSON structure, and being stringified before sending it over the network.
This is the structure of each message:
export interface OperationMessage {
payload?: any;
id?: string;
type: string;
}
Install
pip install ob_graphql
- Ob-Graphql depends on websocket-client is a WebSocket client for Python. It provides access to low level APIs for WebSockets.
Examples
Setup subscriptions super easily
from ob_graphql import OBQLClient
query = """
subscription {
notifications {
id
title
content
}
}
"""
def callback(_id, data):
print("got new data..")
print(f"msg id: {_id}. data: {data}")
with OBQLClient("ws://localhost:8080/graphql") as client:
sub_id = client.subscribe(query, callback=callback)
client.stop_subscribe(sub_id)
Variables can be passed
from ob_graphql import OBQLClient
query = """
subscription ($limit: Int!) {
notifications (order_by: {created: "desc"}, limit: $limit) {
id
title
content
}
}
"""
def callback(_id, data):
print("got new data..")
print(f"msg id: {_id}. data: {data}")
with OBQLClient('ws://localhost:8080/graphql') as client:
sub_id = client.subscribe(query, variables={'limit': 10}, callback=callback)
Headers can be passed too
from ob_graphql import OBQLClient
query = """
subscription ($limit: Int!) {
notifications (order_by: {created: "desc"}, limit: $limit) {
id
title
content
}
}
"""
def callback(_id, data):
print("got new data..")
print(f"msg id: {_id}. data: {data}")
with OBQLClient("ws://localhost:8080/graphql") as client:
sub_id = client.subscribe(
query,
variables={"limit": 10},
headers={"Authorization": "Bearer xxxx"},
callback=callback,
)
client.stop_subscribe(sub_id)
Normal queries and mutations work too
from ob_graphql import OBQLClient
query = """
query ($limit: Int!) {
notifications (order_by: {created: "desc"}, limit: $limit) {
id
title
content
}
}
"""
with OBQLClient('ws://localhost:8080/graphql') as client:
res = client.query(query, variables={'limit': 10}, headers={'Authorization': 'Bearer xxxx'})
print(res)
Without the context manager API
from ob_graphql import OBQLClient
query = """
query ($limit: Int!) {
notifications (order_by: {created: "desc"}, limit: $limit) {
id
title
content
}
}
"""
client = OBQLClient('ws://localhost:8080/graphql')
res = client.query(query, variables={'limit': 10}, headers={'Authorization': 'Bearer xxxx'})
print(res)
client.close()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
ob_graphql-0.0.1.tar.gz
(18.1 kB
view hashes)
Built Distribution
Close
Hashes for ob_graphql-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 317b54990a207a4997f1bfbf1d7205076f6d9b646304efdfa4d4aaae714c8a32 |
|
MD5 | b799f12f3cfb176000b5827c0c77792c |
|
BLAKE2b-256 | 703443220ca442c3fd31045580558513358236a0a942477021a7ee1b01771364 |