Skip to main content

aiohttp based GraphQL client with file upload support

Project description

Asynchronous/IO GraphQL client

PyPI PyPI - Downloads PyPI - License

A Python asynchronous/IO GraphQL client based on aiohttp that supports the GraphQL multipart form requests spec for file uploads.

Requirements

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

  1. Clone the repo
  2. Create and enter a dedicated virtual environment
  3. Run make install-dev to install dev dependencies and a pre-commit hook that automatically formats code.

If you do not have make installed, either install it or take a look at the Makefile to find out how to run individual commands manually.

Formatting and linting

  • Run make format to format the code.
  • Run make lint to lint the code.

Testing

  • Run make test to run all tests in your local environment.
  • Run make test-with-tox to run all tests with all supported python versions.

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

aiogqlc-2.0.0.tar.gz (5.2 kB view hashes)

Uploaded source

Built Distribution

aiogqlc-2.0.0-py3-none-any.whl (5.2 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page