Coder Social home page Coder Social logo

doctorjohn / aiogqlc Goto Github PK

View Code? Open in Web Editor NEW
18.0 2.0 3.0 699 KB

Python aynchronous/IO GraphQL client with file upload and subscription support.

Home Page: https://doctorjohn.github.io/aiogqlc/

License: MIT License

Python 98.99% Makefile 1.01%
python python3 asyncio aiohttp aiohttp-client graphql graphql-client graphql-subscriptions graphql-library graphql-ws

aiogqlc's Introduction

Asynchronous/IO GraphQL client

Versions PyPI Downloads Codecov License

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.

Read the documentation

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)

Read the documentation to learn more about queries, mutations, subscriptions, file uploads and even authentication.

aiogqlc's People

Contributors

doctorjohn avatar fabaff avatar kamadorueda avatar kousu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

kousu hkbluesky

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.