Skip to main content

A simple GraphQL client that works over Websocket as the transport protocol, instead of HTTP.

Project description

Ob-Graphql

Linting Test

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


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 details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ob_graphql-0.0.1-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file ob_graphql-0.0.1.tar.gz.

File metadata

  • Download URL: ob_graphql-0.0.1.tar.gz
  • Upload date:
  • Size: 18.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.27.1

File hashes

Hashes for ob_graphql-0.0.1.tar.gz
Algorithm Hash digest
SHA256 f2f32f2a002ad816792c423f2d28e9da1ddde700695171e9b1c46ffb67d8053e
MD5 bdaecc82a2c0c8a20a7151a40ad02b86
BLAKE2b-256 b6e1e723a757f72c06d6b52fd7b9d2b908aae0f334ea376d404a84ef12d9e887

See more details on using hashes here.

File details

Details for the file ob_graphql-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: ob_graphql-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.27.1

File hashes

Hashes for ob_graphql-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 317b54990a207a4997f1bfbf1d7205076f6d9b646304efdfa4d4aaae714c8a32
MD5 b799f12f3cfb176000b5827c0c77792c
BLAKE2b-256 703443220ca442c3fd31045580558513358236a0a942477021a7ee1b01771364

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page