aiohttp based GraphQL client with file upload support
Project description
Asynchronous/IO GraphQL client
A Python asynchronous/IO GraphQL client based on aiohttp
that supports the GraphQL multipart form requests spec for file uploads.
Requirements
- Python 3
- aiohttp
Installation
pip install aiogqlc
Usage
Executing simple queries
import asyncio
import aiohttp
from aiogqlc import GraphQLClient
query = '''
query {
allFilms {
title
}
}
'''
async def foo():
async with aiohttp.ClientSession() as session:
client = GraphQLClient('https://swapi.graph.cool/', session=session)
response = await client.execute(query)
print(await response.json())
if __name__ == '__main__':
asyncio.get_event_loop().run_until_complete(foo())
Adding authorization headers
import aiohttp
from aiogqlc import GraphQLClient
headers = {
'Authorization': 'Token <your-token-here>'
}
async def foo():
async with aiohttp.ClientSession(headers=headers) as session:
client = GraphQLClient('https://example.com/graphql/', session=session)
Single file upload
import aiohttp
from aiogqlc import GraphQLClient
query = '''
mutation($file: Upload!) {
yourSingleUploadMutation(file: $file) {
errors {
field
messages
}
}
}
'''
variables = {
'file': open('test.zip', 'rb'),
}
async def foo():
async with aiohttp.ClientSession() as session:
client = GraphQLClient('https://example.com/graphql/', session=session)
response = await client.execute(query, variables=variables)
print(await response.json())
Multiple file upload
import aiohttp
from aiogqlc import GraphQLClient
query = '''
mutation($files: [Upload!]!) {
yourMultiUploadMutation(files: $files) {
errors {
field
messages
}
}
}
'''
variables = {
'files': [
open('foo.zip', 'rb'),
open('var.zip', 'rb'),
],
}
async def foo():
async with aiohttp.ClientSession() as session:
client = GraphQLClient('https://example.com/graphql/', session=session)
response = await client.execute(query, variables=variables)
print(await response.json())
Contributing
Quickstart
- Clone the repo
- Create and enter a dedicated virtual environment
- Run
make install-dev
to install dev dependencies and a formatting pre-commit hook.
Individual commands
- Run
pip install -r ".[dev]"
to install dev dependencies. - Run
black aiogqlc setup.py
ormake format
to format the code. - Run
flake8 aiogqlc setup.py
ormake lint
to lint the code. - Run
pre-commit install
to install a formatting pre-commit hook.
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-2.0.0b1.tar.gz
(4.8 kB
view hashes)
Built Distribution
Close
Hashes for aiogqlc-2.0.0b1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 622b8860a7e2d507a6e7b3a490203409f3ad0d2926d9a8826264018c770473da |
|
MD5 | 2fb490cdf5c4e275808a7e7f12e1a6d7 |
|
BLAKE2b-256 | b5a3b429e84bfe8a8b6375d50a5a24955fba71e1c04352c9feb1e5dccf284b71 |