# py-graphql-client
Dead-simple to use GraphQL client over websocket. Using the
[apollo-transport-ws](https://github.com/apollographql/subscriptions-transport-ws/blob/master/PROTOCOL.md)
protocol.
## Install
```bash
pip install py-graphql-client
```
## Examples
### Setup subscriptions super easily
```python
from graphql_client import GraphQLClient
query = """
subscription {
notifications {
id
title
content
}
}
"""
with GraphQLClient('ws://localhost:8080/graphql') as client:
sub_id = client.subscribe(query, callback=callback)
# do other stuff
# ...
# later stop the subscription
client.stop_subscribe(sub_id)
def callback(_id, data):
print("got new data..")
print(f"msg id: {_id}. data: {data}")
```
### Variables can be passed
```python
from graphql_client import GraphQLClient
query = """
subscription ($limit: Int!) {
notifications (order_by: {created: "desc"}, limit: $limit) {
id
title
content
}
}
"""
with GraphQLClient('ws://localhost:8080/graphql') as client:
sub_id = client.subscribe(query, variables={'limit': 10}, callback=callback)
# ...
def callback(_id, data):
print("got new data..")
print(f"msg id: {_id}. data: {data}")
```
### Headers can be passed too
```python
from graphql_client import GraphQLClient
query = """
subscription ($limit: Int!) {
notifications (order_by: {created: "desc"}, limit: $limit) {
id
title
content
}
}
"""
with GraphQLClient('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)
def callback(_id, data):
print("got new data..")
print(f"msg id: {_id}. data: {data}")
```
### Normal queries and mutations work too
```python
from graphql_client import GraphQLClient
query = """
query ($limit: Int!) {
notifications (order_by: {created: "desc"}, limit: $limit) {
id
title
content
}
}
"""
with GraphQLClient('ws://localhost:8080/graphql') as client:
res = client.query(query, variables={'limit': 10}, headers={'Authorization': 'Bearer xxxx'})
print(res)
```
### Without the context manager API
```python
from graphql_client import GraphQLClient
query = """
query ($limit: Int!) {
notifications (order_by: {created: "desc"}, limit: $limit) {
id
title
content
}
}
"""
client = GraphQLClient('ws://localhost:8080/graphql')
res = client.query(query, variables={'limit': 10}, headers={'Authorization': 'Bearer xxxx'})
print(res)
client.close()
```
## TODO
- support http as well
- should use asyncio websocket library?
Raw data
{
"_id": null,
"home_page": "https://github.com/ecthiender/py-graphql-client",
"name": "py-graphql-client",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.4",
"maintainer_email": "",
"keywords": "graphql,websocket,subscriptions,graphql-client",
"author": "Anon Ray",
"author_email": "rayanon004@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/51/b8/bfc6e04091871d2617892c21411d0331fcae95102e80db8eb493641abee7/py-graphql-client-0.1.2.tar.gz",
"platform": null,
"description": "# py-graphql-client\nDead-simple to use GraphQL client over websocket. Using the\n[apollo-transport-ws](https://github.com/apollographql/subscriptions-transport-ws/blob/master/PROTOCOL.md)\nprotocol.\n\n## Install\n\n```bash\npip install py-graphql-client\n```\n\n## Examples\n\n### Setup subscriptions super easily\n\n```python\nfrom graphql_client import GraphQLClient\n\nquery = \"\"\"\n subscription {\n notifications {\n id\n title\n content\n }\n }\n\"\"\"\n\nwith GraphQLClient('ws://localhost:8080/graphql') as client:\n\n sub_id = client.subscribe(query, callback=callback)\n # do other stuff\n # ...\n # later stop the subscription\n client.stop_subscribe(sub_id)\n\ndef callback(_id, data):\n print(\"got new data..\")\n print(f\"msg id: {_id}. data: {data}\")\n```\n\n### Variables can be passed\n\n```python\nfrom graphql_client import GraphQLClient\n\nquery = \"\"\"\n subscription ($limit: Int!) {\n notifications (order_by: {created: \"desc\"}, limit: $limit) {\n id\n title\n content\n }\n }\n \"\"\"\n\nwith GraphQLClient('ws://localhost:8080/graphql') as client:\n sub_id = client.subscribe(query, variables={'limit': 10}, callback=callback)\n # ...\n\ndef callback(_id, data):\n print(\"got new data..\")\n print(f\"msg id: {_id}. data: {data}\")\n\n```\n\n### Headers can be passed too\n\n```python\nfrom graphql_client import GraphQLClient\n\nquery = \"\"\"\n subscription ($limit: Int!) {\n notifications (order_by: {created: \"desc\"}, limit: $limit) {\n id\n title\n content\n }\n }\n \"\"\"\n\nwith GraphQLClient('ws://localhost:8080/graphql') as client:\n sub_id = client.subscribe(query,\n variables={'limit': 10},\n headers={'Authorization': 'Bearer xxxx'},\n callback=callback)\n ...\n client.stop_subscribe(sub_id)\n\ndef callback(_id, data):\n print(\"got new data..\")\n print(f\"msg id: {_id}. data: {data}\")\n```\n\n### Normal queries and mutations work too\n\n```python\nfrom graphql_client import GraphQLClient\n\nquery = \"\"\"\n query ($limit: Int!) {\n notifications (order_by: {created: \"desc\"}, limit: $limit) {\n id\n title\n content\n }\n }\n\"\"\"\n\nwith GraphQLClient('ws://localhost:8080/graphql') as client:\n res = client.query(query, variables={'limit': 10}, headers={'Authorization': 'Bearer xxxx'})\n print(res)\n```\n\n### Without the context manager API\n\n```python\nfrom graphql_client import GraphQLClient\n\nquery = \"\"\"\n query ($limit: Int!) {\n notifications (order_by: {created: \"desc\"}, limit: $limit) {\n id\n title\n content\n }\n }\n\"\"\"\n\nclient = GraphQLClient('ws://localhost:8080/graphql')\nres = client.query(query, variables={'limit': 10}, headers={'Authorization': 'Bearer xxxx'})\nprint(res)\nclient.close()\n```\n\n\n## TODO\n- support http as well\n- should use asyncio websocket library?\n",
"bugtrack_url": null,
"license": "BSD3",
"summary": "A dead-simple GraphQL client that supports subscriptions over websockets",
"version": "0.1.2",
"project_urls": {
"Homepage": "https://github.com/ecthiender/py-graphql-client"
},
"split_keywords": [
"graphql",
"websocket",
"subscriptions",
"graphql-client"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "adfa358c3dd0dd59c36d05bb2fc406b55f30e33e350025d01efb61544333c7f0",
"md5": "5bf091ae66aff171534f2d1d94f66454",
"sha256": "df5d0ed3151025b959405dd6caed7dcb772a7180e8181abf8cc2a66b6aa8d7bb"
},
"downloads": -1,
"filename": "py_graphql_client-0.1.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "5bf091ae66aff171534f2d1d94f66454",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.4",
"size": 6317,
"upload_time": "2023-05-27T13:15:32",
"upload_time_iso_8601": "2023-05-27T13:15:32.026712Z",
"url": "https://files.pythonhosted.org/packages/ad/fa/358c3dd0dd59c36d05bb2fc406b55f30e33e350025d01efb61544333c7f0/py_graphql_client-0.1.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "51b8bfc6e04091871d2617892c21411d0331fcae95102e80db8eb493641abee7",
"md5": "0fc3766cb679f58beb7ff78daf74489e",
"sha256": "70484d64457f4868e2f31b0308cc9cb43a4599ed0b546a6042f9af90ab541bbf"
},
"downloads": -1,
"filename": "py-graphql-client-0.1.2.tar.gz",
"has_sig": false,
"md5_digest": "0fc3766cb679f58beb7ff78daf74489e",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.4",
"size": 6266,
"upload_time": "2023-05-27T13:15:34",
"upload_time_iso_8601": "2023-05-27T13:15:34.903842Z",
"url": "https://files.pythonhosted.org/packages/51/b8/bfc6e04091871d2617892c21411d0331fcae95102e80db8eb493641abee7/py-graphql-client-0.1.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-05-27 13:15:34",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "ecthiender",
"github_project": "py-graphql-client",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "py-graphql-client"
}