An asyncio GraphQL client built on top of aiohttp and graphql-core-next
Project description
Asynchronous GraphQL Client
An asynchronous GraphQL client built on top of aiohttp and graphql-core-next. It supports multiple transports, including aiohttp (default) and httpx. The client by default introspects schemas and validates all queries prior to dispatching to the server.
Documentation
For the most recent project documentation, you can visit https://aiographql-client.readthedocs.io/.
Installation
To install the client with the default aiohttp transport:
pip install aiographql-client[aiohttp]
To use httpx as the transport (includes websockets for subscriptions):
pip install aiographql-client[httpx]
To install the client with the default aiohttp transport and pydantic support:
pip install aiographql-client[aiohttp,pydantic]
Transports & Auto-detection
The client supports multiple HTTP backends. You can choose which one to use during installation via extras.
aiohttp: The default asynchronous HTTP client for the library. Supports both queries and subscriptions.httpx: A modern, feature-rich HTTP client. Supports queries and subscriptions (viawebsockets).
Auto-detection
When you initialize a GraphQLClient without specifying a transport, it will try to automatically detect an available backend:
HTTP Transport (Queries/Mutations)
- It first checks if
aiohttpis installed. - If not, it checks if
httpxis installed. - If neither is found, it raises a
RuntimeError.
Subscription Transport
- It first checks if
aiohttpis installed. - If not, it checks if
websocketsis installed. - If neither is found, it raises a
RuntimeError.
Note that websockets is installed automatically when the httpx extra is used.
from aiographql.client import GraphQLClient
# Auto-detects available transport (aiohttp preferred if both installed)
client = GraphQLClient(endpoint="https://api.github.com/graphql")
Manual Selection
You can also explicitly specify which transport to use by providing a transport instance:
from aiographql.client import GraphQLClient
from aiographql.client.transport import HttpxTransport, AiohttpTransport, WebsocketSubscriptionTransport
# Explicitly use httpx
transport = HttpxTransport(endpoint="https://api.github.com/graphql")
client = GraphQLClient(
endpoint="https://api.github.com/graphql",
transport=transport
)
# Explicitly use aiohttp
transport = AiohttpTransport(endpoint="https://api.github.com/graphql")
client = GraphQLClient(
endpoint="https://api.github.com/graphql",
transport=transport
)
# Explicitly use websockets for subscriptions
from aiographql.client.transport.websocket import WebsocketSubscriptionTransport
subscription_transport = WebsocketSubscriptionTransport(endpoint="wss://your-api.com/graphql")
client = GraphQLClient(
endpoint="https://your-api.com/graphql",
subscription_transport=subscription_transport
)
Development
To run the full test suite with all supported transport combinations and Python versions, you can use tox:
# Install tox
pip install tox
# Run all environments
tox
# Run a specific environment (e.g., Python 3.10 with aiohttp and pydantic)
tox -e py310-aiohttp-pydantic
# Run with only aiohttp
tox -e py310-aiohttp
# Run with only httpx
tox -e py310-httpx
See pyproject.toml for all available environment factors and combinations.
Example Usage
Here are some example usages of this client implementation. For more examples, and advanced scenarios, see Usage Examples section in the documentation or the examples directory in this repository.
Examples
We provide several well-documented, scenario-based examples using a Strawberry GraphQL server. You can find them in the examples/ directory.
To run the examples, first start the Strawberry server using podman compose:
podman compose run --build strawberry-server
Then you can run any of the scenario scripts:
# Basic Queries
poetry run python examples/basic_queries.py
# Pydantic Data Models
poetry run python examples/data_models.py
# Dataclass Models
poetry run python examples/dataclass_models.py
# Authenticated Requests
poetry run python examples/authenticated_requests.py
# Mutations with Variables
poetry run python examples/mutations.py
# Real-time Subscriptions
poetry run python examples/subscriptions.py
# Httpx Transport & Shared Sessions
poetry run python examples/httpx_transport.py
# Custom Serialization & Codecs
poetry run python examples/custom_serialization.py
See examples/README.md for more detailed descriptions of each scenario.
Simple Query
async def get_logged_in_username(token: str) -> GraphQLResponse:
client = GraphQLClient(
endpoint="https://api.github.com/graphql",
headers={"Authorization": f"Bearer {token}"},
)
request = GraphQLRequest(
query="""
query {
viewer {
login
}
}
"""
)
return await client.query(request=request)
>>> import asyncio
>>> response = asyncio.run(get_logged_in_username("<TOKEN FROM GITHUB GRAPHQL API>"))
>>> response.data
{'viewer': {'login': 'username'}}
Query Subscription
async def print_city_updates(client: GraphQLClient, city: str) -> None:
request = GraphQLRequest(
query="""
subscription ($city:String!) {
city(where: {name: {_eq: $city}}) {
description
id
}
}
""",
variables={"city": city},
)
# subscribe to data and error events, and print them
await client.subscribe(
request=request, on_data=print, on_error=print, wait=True
)
For custom event specific callback registration, see Callback Registry Documentation.
Query Validation Failures
If your query is invalid, thanks to graphql-core-next, we get a detailed exception in the traceback.
aiographql.client.exceptions.GraphQLClientValidationException: Query validation failed
Cannot query field 'ids' on type 'chatbot'. Did you mean 'id'?
GraphQL request (4:13)
3: chatbot {
4: ids, bot_names
^
5: }
Cannot query field 'bot_names' on type 'chatbot'. Did you mean 'bot_name' or 'bot_language'?
GraphQL request (4:18)
3: chatbot {
4: ids, bot_names
^
5: }
Query Variables & Operations
Support for multi-operation requests and variables is available via the client. For example, the following request contains multiple operations. The instance specifies default values to use.
request = GraphQLRequest(
query="""
query get_bot_created($id: Int) {
chatbot(where: {id: {_eq: $id}}) {
id, created
}
}
query get_bot_name($id: Int) {
chatbot(where: {id: {_eq: $id}}) {
id, bot_name
}
}
""",
variables={"id": 109},
operation="get_bot_name"
)
The default values can be overridden at the time of making the request if required.
await client.query(request=request, variables={"id": 20}, operation="get_bot_created")
Typed Usage (Bring Your Own Models)
The client supports explicit decoding of results into your own models, like dataclasses or Pydantic (v2) models.
from pydantic import BaseModel
from aiographql.client import GraphQLClient
class User(BaseModel):
id: int
name: str
client = GraphQLClient(endpoint="http://localhost/graphql")
# Explicitly decode into a Pydantic model
user = await client.query_data_as(
"{ user(id: 1) { id name } }",
User,
path="user"
)
print(user.name) # Alice
Pass models directly into variables:
# Model as variable
user_input = CreateUserInput(name="Alice")
response = await client.query(query, variables={"input": user_input})
See Data Models Documentation for more information.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file aiographql_client-1.2.0.tar.gz.
File metadata
- Download URL: aiographql_client-1.2.0.tar.gz
- Upload date:
- Size: 24.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f6581610dd28ec7c1e3310c252645d79b550dab4dec7808e3a680a46beb49e29
|
|
| MD5 |
8bab3e95a69341718bb789ee9a9c5f97
|
|
| BLAKE2b-256 |
516a966e9f812b9bd852bc6e016dd8719ca7b7f8a85697c8d45b511ef1a46873
|
Provenance
The following attestation bundles were made for aiographql_client-1.2.0.tar.gz:
Publisher:
release.yml on abn/aiographql-client
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
aiographql_client-1.2.0.tar.gz -
Subject digest:
f6581610dd28ec7c1e3310c252645d79b550dab4dec7808e3a680a46beb49e29 - Sigstore transparency entry: 1636005536
- Sigstore integration time:
-
Permalink:
abn/aiographql-client@236654f8b91697a66d7948f4b8153f52ed7a7d78 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/abn
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@236654f8b91697a66d7948f4b8153f52ed7a7d78 -
Trigger Event:
push
-
Statement type:
File details
Details for the file aiographql_client-1.2.0-py3-none-any.whl.
File metadata
- Download URL: aiographql_client-1.2.0-py3-none-any.whl
- Upload date:
- Size: 28.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
16d60083aa756c9677c4a2aacd5220f5adc4b4387a716151ec0fa9b0a1a67c02
|
|
| MD5 |
392fd0e7e07d534265e4e318b1bc2999
|
|
| BLAKE2b-256 |
c965bc5def7a04b0d1dcee8926c3ecd1f61ad698d352b6ab64d71dbe1f74af4b
|
Provenance
The following attestation bundles were made for aiographql_client-1.2.0-py3-none-any.whl:
Publisher:
release.yml on abn/aiographql-client
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
aiographql_client-1.2.0-py3-none-any.whl -
Subject digest:
16d60083aa756c9677c4a2aacd5220f5adc4b4387a716151ec0fa9b0a1a67c02 - Sigstore transparency entry: 1636005633
- Sigstore integration time:
-
Permalink:
abn/aiographql-client@236654f8b91697a66d7948f4b8153f52ed7a7d78 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/abn
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@236654f8b91697a66d7948f4b8153f52ed7a7d78 -
Trigger Event:
push
-
Statement type: