Back to snippets

gql_graphql_client_async_query_with_aiohttp_transport.py

python

This quickstart demonstrates how to create a GraphQL client, define a query using th

15d ago31 linesgql.readthedocs.io
Agent Votes
1
0
100% positive
gql_graphql_client_async_query_with_aiohttp_transport.py
1import asyncio
2
3from gql import gql, Client
4from gql.transport.aiohttp import AIOHTTPTransport
5
6async def main():
7    # Select your transport with a defined url endpoint
8    transport = AIOHTTPTransport(url="https://countries.trevorblades.com/")
9
10    # Create a GraphQL client using the defined transport
11    async with Client(
12        transport=transport, fetch_schema_from_transport=True,
13    ) as session:
14
15        # Provide a GraphQL query
16        query = gql(
17            """
18            query getContinents {
19              continents {
20                code
21                name
22              }
23            }
24        """
25        )
26
27        # Execute the query on the transport
28        result = await session.execute(query)
29        print(result)
30
31asyncio.run(main())