Skip to main content

An asyncio GraphQL client built on top of aiohttp and graphql-core-next

Project description

Asyncio GraphQL Client

PyPI version Code style: black License: MIT Sonarcloud Status CircleCI

An asyncio GraphQL client built on top of aiohttp and graphql-core-next. The client by default introspects schemas and validates all queries prior to dispatching to the server.

Installation

pip install aiographql-client

Example Usage

Here are some example usages of this client implementation. These examples use the Hasura GraphQL Engine.

Simple Query

async def get_bots():
    client = GraphQLClient(
        endpoint="http://localhost:8080/v1alpha1/graphql",
        headers={"x-hasura-admin-secret": "myadminsecretkey"},
    )
    request = GraphQLRequest(
        query="""
        query get_bots {
          chatbot {
            id, bot_name
          }
        }
        """
    )
    transaction = await client.post(request)

    # display the query used
    print(transaction.request.query)

    # dump the response data
    print(transaction.response.data)

Query Subscription

async def get_bots():
    client = GraphQLClient(
        endpoint="http://localhost:8080/v1alpha1/graphql",
        headers={"x-hasura-admin-secret": "myadminsecretkey"},
    )
    request = GraphQLRequest(
        query="""
        subscription get_bot_updates {
          chatbot {
            id, bot_name
          }
        }
        """
    )
    
    # configure callbacks, here we simply print the event message when a data event
    # (`GraphQLSubscriptionEvent`) is received.
    callbacks = CallbackRegistry()
    callbacks.register(
        GraphQLSubscriptionEventType.DATA, lambda event: print(event.message)
    )
    
    subscription: GraphQLSubscription = await client.subscribe(request, callbacks)
    await subscription.task

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:           }

Sample Requests

Using variables and operationName

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'},
    operationName='get_bot_name'
)

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

aiographql-client-0.4.0.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

aiographql_client-0.4.0-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file aiographql-client-0.4.0.tar.gz.

File metadata

  • Download URL: aiographql-client-0.4.0.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.17 CPython/3.7.5 Linux/5.0.0-1023-azure

File hashes

Hashes for aiographql-client-0.4.0.tar.gz
Algorithm Hash digest
SHA256 79d87d8e27b07b8b7672d55d20659e982eafbc696096ba1d511dcafa454ec37b
MD5 222c9faf0d2d3902f90d68a1b992d1e3
BLAKE2b-256 918899334a6aca3f1a33bd0a6b655b83366101b278dfbb04fcc884c2f4b73271

See more details on using hashes here.

File details

Details for the file aiographql_client-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: aiographql_client-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.17 CPython/3.7.5 Linux/5.0.0-1023-azure

File hashes

Hashes for aiographql_client-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d8ce7f2d9ab0e95fff58b97868d60eca46cdd4b06014a6eab37f0d5a7582f470
MD5 77672d354533310c39de11696c0360b1
BLAKE2b-256 4dffb91cd80e185198a7ea1611107e1438a9bde58a066884303f9647c4e28a51

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page