aiohttp based GraphQL client
Project description
Asynchronous/IO GraphQL client
A Python asynchronous/IO GraphQL client based on aiohttp.
In addition to standard HTTP POST queries
and mutations
this client fully supports
the GraphQL multipart form requests spec for file uploads
and the graphql-ws subprotocol for WebSocket based subscriptions
.
Installation
pip install aiogqlc
Basic usage
Check the documentation for detailed and more advanced usage examples.
Queries
import asyncio
import aiohttp
from aiogqlc import GraphQLClient
ENDPOINT = "https://swapi-graphql.netlify.app/.netlify/functions/index"
document = """
query {
allFilms {
films {
title
}
}
}
"""
async def main():
async with aiohttp.ClientSession() as session:
client = GraphQLClient(ENDPOINT, session=session)
response = await client.execute(document)
print(await response.json())
if __name__ == "__main__":
asyncio.run(main())
Mutations
import aiohttp
from aiogqlc import GraphQLClient
document = """
mutation ($userId: ID!) {
deleteUser (id: $userId) {
id
}
}
"""
variables = {
"userId": "42",
}
async def main():
async with aiohttp.ClientSession() as session:
client = GraphQLClient("https://example.com/graphql/", session=session)
response = await client.execute(document, variables=variables)
print(await response.json())
File uploads
import aiohttp
from aiogqlc import GraphQLClient
document = """
mutation($file: Upload!) {
uploadFile(file: $file) {
size
}
}
"""
variables = {
"file": open("test.txt", "rb")
}
async def foo():
async with aiohttp.ClientSession() as session:
client = GraphQLClient("https://example.com/graphql/", session=session)
response = await client.execute(document, variables=variables)
print(await response.json())
Subscriptions
import aiohttp
from aiogqlc import GraphQLClient
document = """
subscription($postId: ID!) {
likeAdded(postId: $postId)
}
"""
variables = {
"postId": "42"
}
async def main():
async with aiohttp.ClientSession() as session:
client = GraphQLClient("https://example.com/graphql/", session=session)
async with client.connect() as connection:
async for payload in connection.subscribe(document, variables=variables):
print(payload)
Documentation
Read the documentation to learn more about queries, mutations, subscriptions, file uploads and even authentication.
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
aiogqlc-5.3.0.tar.gz
(7.4 kB
view details)
Built Distribution
File details
Details for the file aiogqlc-5.3.0.tar.gz
.
File metadata
- Download URL: aiogqlc-5.3.0.tar.gz
- Upload date:
- Size: 7.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.12.7 Linux/6.11.5-arch1-1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 24ff2527c0a1a3c8ebccbbd1ff3f9b9a00ef8120a5d95e8d75d12316ed8d3feb |
|
MD5 | 399d118592ff76edaf7c77d7dc1cc1eb |
|
BLAKE2b-256 | 945daaa425824adcc752d9aa8424858c004c7348d9cec5727e9d46001a7b42dc |
File details
Details for the file aiogqlc-5.3.0-py3-none-any.whl
.
File metadata
- Download URL: aiogqlc-5.3.0-py3-none-any.whl
- Upload date:
- Size: 7.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.12.7 Linux/6.11.5-arch1-1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 32ddaa52996e289fd56a76b7cc4d4f7c0c44d8bc352c7b4ac60f2ae5dacbc0d6 |
|
MD5 | fa4ed32a22e682f045baee47a243d0d6 |
|
BLAKE2b-256 | 8bcf69618f8d74cbcf855f92a2f8f7b969d7d8e4c5fbc4d706eef3cebc4e0cd5 |