Skip to main content

GraphQL client for Python

Project description

GQL

This is a GraphQL client for Python. Plays nicely with graphene, graphql-core, graphql-js and any other GraphQL implementation compatible with the GraphQL specification.

GQL architecture is inspired by React-Relay and Apollo-Client.

GitHub-Actions pyversion pypi Anaconda-Server Badge codecov

Documentation

The complete documentation for GQL can be found at gql.readthedocs.io.

Features

Installation

You can install GQL with all the optional dependencies using pip:

# Quotes may be required on certain shells such as zsh.
pip install "gql[all]"

NOTE: See also the documentation to install GQL with less extra dependencies depending on the transports you would like to use or for alternative installation methods.

Usage

Sync usage

from gql import Client, gql
from gql.transport.aiohttp import AIOHTTPTransport

# Select your transport with a defined url endpoint
transport = AIOHTTPTransport(url="https://countries.trevorblades.com/")

# Create a GraphQL client using the defined transport
client = Client(transport=transport)

# Provide a GraphQL query
query = gql(
    """
    query getContinents {
      continents {
        code
        name
      }
    }
"""
)

# Execute the query on the transport
result = client.execute(query)
print(result)

Executing the above code should output the following result:

$ python basic_example.py
{'continents': [{'code': 'AF', 'name': 'Africa'}, {'code': 'AN', 'name': 'Antarctica'}, {'code': 'AS', 'name': 'Asia'}, {'code': 'EU', 'name': 'Europe'}, {'code': 'NA', 'name': 'North America'}, {'code': 'OC', 'name': 'Oceania'}, {'code': 'SA', 'name': 'South America'}]}

WARNING: Please note that this basic example won't work if you have an asyncio event loop running. In some python environments (as with Jupyter which uses IPython) an asyncio event loop is created for you. In that case you should use instead the async usage example.

Async usage

import asyncio

from gql import Client, gql
from gql.transport.aiohttp import AIOHTTPTransport


async def main():

    # Select your transport with a defined url endpoint
    transport = AIOHTTPTransport(url="https://countries.trevorblades.com/graphql")

    # Create a GraphQL client using the defined transport
    client = Client(transport=transport)

    # Provide a GraphQL query
    query = gql(
        """
        query getContinents {
          continents {
            code
            name
          }
        }
    """
    )

    # Using `async with` on the client will start a connection on the transport
    # and provide a `session` variable to execute queries on this connection
    async with client as session:

        # Execute the query
        result = await session.execute(query)
        print(result)


asyncio.run(main())

Contributing

See CONTRIBUTING.md

License

MIT License

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

gql-4.4.0b0.tar.gz (233.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gql-4.4.0b0-py3-none-any.whl (96.2 kB view details)

Uploaded Python 3

File details

Details for the file gql-4.4.0b0.tar.gz.

File metadata

  • Download URL: gql-4.4.0b0.tar.gz
  • Upload date:
  • Size: 233.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for gql-4.4.0b0.tar.gz
Algorithm Hash digest
SHA256 78ca532335ca4716fa56ae68676d9652d7efd88080a0b35c97bbddd9b6a51151
MD5 d0f0c0f53d88944039d485c0ed1cf8a1
BLAKE2b-256 66abaff4fbebcc16fe4302ed844d896a7a5af9da0232c01ddfe4796bdc418e8d

See more details on using hashes here.

File details

Details for the file gql-4.4.0b0-py3-none-any.whl.

File metadata

  • Download URL: gql-4.4.0b0-py3-none-any.whl
  • Upload date:
  • Size: 96.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for gql-4.4.0b0-py3-none-any.whl
Algorithm Hash digest
SHA256 5884cbc9d86da3f1b0324f56724494b376016f6842e7991c8c8471761dd29c72
MD5 583eb43435c7df77147000e1d701f1d1
BLAKE2b-256 3e9700105a44b645f2234e4540027d2ae4ea34b6a95d40e45f9f01c020d0f5b0

See more details on using hashes here.

Supported by

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