gql

GraphQL client for Python

MIT 53 个版本 Python >=3.8.1
Syrus Akbary <me@syrusakbary.com>
安装
pip install gql
poetry add gql
pipenv install gql
conda install gql
描述

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

版本列表
4.3.0b3 2026-05-30
4.3.0b2 2026-04-02
4.3.0b1 2026-03-30
4.3.0b0 2026-01-09
4.2.0b0 2025-09-05
4.1.0b0 2025-08-17
4.0.0 2025-08-17
4.0.0b0 2025-05-28
4.0.0a0 2025-05-20
3.6.0b4 2025-02-18
3.6.0b3 2025-01-20
3.6.0b2 2024-04-14
3.6.0b1 2024-02-08
3.6.0b0 2024-01-03
3.5.3 2025-05-20
3.5.2 2025-03-06
3.5.1 2025-02-19
3.5.0 2024-01-03
3.5.0b9 2023-12-15
3.5.0b8 2023-11-19
3.5.0b7 2023-11-14
3.5.0b6 2023-10-04
3.5.0b5 2023-07-26
3.5.0b4 2023-05-06
3.5.0b3 2023-02-23
3.5.0b2 2023-02-23
3.5.0b1 2023-02-22
3.5.0b0 2022-11-26
3.5.0a0 2022-11-07
3.4.1 2023-05-06
3.4.0 2022-07-14
3.3.0 2022-05-20
3.2.0 2022-04-12
3.1.0 2022-03-11
3.0.0 2022-01-22
3.0.0rc1 2022-01-16
3.0.0rc0 2021-12-10
3.0.0b1 2021-11-22
3.0.0b0 2021-10-26
3.0.0a6 2021-06-09
3.0.0a5 2020-11-21
3.0.0a4 2020-11-01
3.0.0a3 2020-10-15
3.0.0a2 2020-10-04
3.0.0a1 2020-07-13
3.0.0a0 2020-05-17
2.0.0 2020-05-17
0.5.0 2020-05-10
0.4.0 2020-05-10
0.3.0 2020-01-24
0.2.0 2019-12-29
0.1.0 2016-08-06